const repeatedStringMatch = (A, B) => {
const count = Math.ceil(B.length / A.length)
const str = A.repeat(count)
return str.includes(B) ? count : (str + A).includes(B) ? count + 1 : 1
};
Loc Nguyen
@loctn
LeetCode Stats: https://leetcode.com/loctn/
Posts made by loctn

3line JavaScript O(mn) solution

Simple JavaScript O(n) solution
const validPalindrome = (s) => { for (let i = 0, stop = s.length / 2; i < stop; i++) { let j = s.length  i  1 if (s[i] !== s[j]) { return isPalindrome(cut(s, i))  isPalindrome(cut(s, j)) } } return true }; const cut = (s, i) => s.substr(0, i) + s.substr(i + 1); const isPalindrome = (s) => s === s.split('').reverse().join('');

RE: A few lines of JavaScript
@giltwg because the times when the node is not falsy, there is a value there accessed by
.val
. We useundefined
simply because that is the value of a nonexistent property, in this case theval
property of0
, but we don't prefer it for any other particular reason. Basically, it doesn't matter what the second term is in(t1  0)
 we could have used(t1  {})
 but I chose0
for brevity. 
RE: A few lines of JavaScript
@giltwg we need to add the values when they exist. When they don't exist they are
null
which doesn't have properties sincenull
is not an object but its own type (not obvious becausetypeof null
gives'object'
), so we have to "or" it to0
because(0).val
isundefined
which is falsy and will contribute zero to the sum. 
Inelegant JavaScript solution
const checkPossibility = (nums) => { const left = nums.slice(0) const right = nums.slice(0) const i = nums.findIndex((a, i) => a > nums[i + 1]) if (~i) left.splice(i, 1) if (~i) right.splice(i + 1, 1) return !left.some((a, i) => a > left[i + 1])  !right.some((a, i) => a > right[i + 1]) };
Find the index before the first decrease and see if removing either element creates a nondecreasing array. O(n) time and O(n) space.

JavaScript solution using recursion
const constructMaximumBinaryTree = (nums, low = 0, high = nums.length  1) => { if (low > high) return null let i = low for (let j = low + 1; j <= high; j++) { if (nums[j] > nums[i]) i = j } const root = new TreeNode(nums[i]) root.left = constructMaximumBinaryTree(nums, low, i  1) root.right = constructMaximumBinaryTree(nums, i + 1, high) return root };
O(n²) time and O(n) space worst, O(n log n) time and O(log n) space average.

2line JavaScript solution
const imageSmoother = (M) => { const DIRS = [[1, 1], [1, 0], [1, 1], [0, 1], [0, 0], [0, 1], [1, 1], [1, 0], [1, 1]] return M.map((row, r) => row.map((el, c) => Math.floor( DIRS.reduce((sum, d) => sum + (M[r + d[0]] && M[r + d[0]][c + d[1]]  0) , 0) / (3  !r  (r === M.length  1)) / (3  !c  (c === M[0].length  1)) )) ) };
O(n) space for the result matrix.

Clear JavaScript solution
const lowestCommonAncestor = (root, p, q) => { if (!root  root === p  root === q) return root const left = lowestCommonAncestor(root.left, p, q) const right = lowestCommonAncestor(root.right, p, q) if (!left) return right // p and q are in the right subtree if (!right) return left // p and q are in the left subtree return root // p is in one side and q is in the other };
The basic algorithm has been posted by many, but hopefully these commented lines will help someone.