diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js new file mode 100644 index 000000000..dcdc748ab --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js @@ -0,0 +1,33 @@ +// TC: O(N^2) +// SC: O(N^2) + +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {number[]} preorder + * @param {number[]} inorder + * @return {TreeNode} + */ +var buildTree = function (preorder, inorder) { + if (inorder.length === 0) { + return null; + } + + const rootValue = preorder[0]; + const leftNodeLength = inorder.findIndex((value) => value === rootValue); + const leftNode = buildTree( + preorder.slice(1, 1 + leftNodeLength), + inorder.slice(0, leftNodeLength) + ); + const rightNode = buildTree( + preorder.slice(1 + leftNodeLength), + inorder.slice(leftNodeLength + 1) + ); + return new TreeNode(rootValue, leftNode, rightNode); +}; diff --git a/counting-bits/wogha95.js b/counting-bits/wogha95.js new file mode 100644 index 000000000..1f2ac9e4b --- /dev/null +++ b/counting-bits/wogha95.js @@ -0,0 +1,25 @@ +// TC: O(N) +// SC: O(N) + +/** + * @param {number} n + * @return {number[]} + */ +var countBits = function (n) { + const result = [0]; + let pointer = 0; + let lastPointer = 0; + + for (let num = 1; num <= n; num++) { + result.push(result[pointer] + 1); + + if (pointer === lastPointer) { + lastPointer = result.length - 1; + pointer = 0; + } else { + pointer += 1; + } + } + + return result; +}; diff --git a/decode-ways/wogha95.js b/decode-ways/wogha95.js new file mode 100644 index 000000000..450467e96 --- /dev/null +++ b/decode-ways/wogha95.js @@ -0,0 +1,48 @@ +// TC: O(N) +// SC: O(N) + +/** + * @param {string} s + * @return {number} + */ +var numDecodings = function (s) { + if (s[0] === "0") { + return 0; + } + if (s.length === 1) { + return 1; + } + + const dpTable = new Array(s.length).fill(0); + if (s[0] !== "0") { + dpTable[0] = 1; + } + if (s[1] !== "0") { + dpTable[1] += 1; + } + if (isValid(`${s[0]}${s[1]}`)) { + dpTable[1] += 1; + } + + for (let index = 2; index < s.length; index++) { + if (s[index] !== "0") { + dpTable[index] += dpTable[index - 1]; + } + if (s[index - 1] !== "0" && isValid(`${s[index - 1]}${s[index]}`)) { + dpTable[index] += dpTable[index - 2]; + } + } + + return dpTable[dpTable.length - 1]; + + function isValid(stringNumber) { + const number = Number(stringNumber); + if (number <= 0) { + return false; + } + if (27 <= number) { + return false; + } + return true; + } +}; diff --git a/valid-anagram/wogha95.js b/valid-anagram/wogha95.js new file mode 100644 index 000000000..22b2f74b9 --- /dev/null +++ b/valid-anagram/wogha95.js @@ -0,0 +1,11 @@ +// TC: O(N * log N) +// SC: O(N) + +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + return s.split('').sort().join('') === t.split('').sort().join(''); +};