diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/tolluset.ts b/construct-binary-tree-from-preorder-and-inorder-traversal/tolluset.ts new file mode 100644 index 000000000..3995e72b1 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/tolluset.ts @@ -0,0 +1,36 @@ +class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = val === undefined ? 0 : val; + this.left = left === undefined ? null : left; + this.right = right === undefined ? null : right; + } +} + +/** + * TC: O(n^2) + * SC: O(n) + * */ +function buildTree(preorder: number[], inorder: number[]): TreeNode | null { + if (!preorder?.length || !inorder?.length) { + return null; + } + + const rootValue = preorder[0]; + const root = new TreeNode(rootValue); + const inorderRootIndex = inorder.indexOf(rootValue); + + root.left = buildTree( + preorder.slice(1, inorderRootIndex + 1), + inorder.slice(0, inorderRootIndex), + ); + + root.right = buildTree( + preorder.slice(inorderRootIndex + 1), + inorder.slice(inorderRootIndex + 1), + ); + + return root; +} diff --git a/counting-bits/tolluset.ts b/counting-bits/tolluset.ts new file mode 100644 index 000000000..2d97db47e --- /dev/null +++ b/counting-bits/tolluset.ts @@ -0,0 +1,13 @@ +/* + * TC: O(nlogn) + * SC: O(nlogn) + * */ +function countBits(n: number): number[] { + return Array.from({ length: n + 1 }, (_, i) => i).map( + (v) => + v + .toString(2) + .split("") + .filter((v) => v === "1").length, + ); +} diff --git a/decode-ways/tolluset.ts b/decode-ways/tolluset.ts new file mode 100644 index 000000000..eab623a82 --- /dev/null +++ b/decode-ways/tolluset.ts @@ -0,0 +1,26 @@ +/** + * TC: O(n) + * SC: O(n) + */ +function numDecodings(s: string): number { + const n = s.length; + const dp = new Array(n + 1).fill(0); + + dp[0] = 1; + dp[1] = Number(s[0]) === 0 ? 0 : 1; + + for (let i = 2; i <= n; i++) { + const oneDigit = Number(s.slice(i - 1, i)); + const twoDigits = Number(s.slice(i - 2, i)); + + if (oneDigit >= 1 && oneDigit <= 9) { + dp[i] += dp[i - 1]; + } + + if (twoDigits >= 10 && twoDigits <= 26) { + dp[i] += dp[i - 2]; + } + } + + return dp[n]; +} diff --git a/encode-and-decode-strings/tolluset.ts b/encode-and-decode-strings/tolluset.ts new file mode 100644 index 000000000..145170bbd --- /dev/null +++ b/encode-and-decode-strings/tolluset.ts @@ -0,0 +1,11 @@ +/* + * TC: O(nm) + * SC: O(nm) + * */ +function encode(arr: string[]): string { + return arr.join("🎃"); +} + +function decode(str: string): string[] { + return str.split("🎃"); +} diff --git a/valid-anagram/tolluset.ts b/valid-anagram/tolluset.ts new file mode 100644 index 000000000..c979f0c14 --- /dev/null +++ b/valid-anagram/tolluset.ts @@ -0,0 +1,17 @@ +/* + * TC: O(n) + * SC: O(n) + * */ +function isAnagram(s: string, t: string): boolean { + if (s.length !== t.length) { + return false; + } + + const groupS = groupBy(s); + const groupT = groupBy(t); + + return Object.keys(groupS).every((k) => groupS[k] === groupT[k]); +} + +const groupBy = (v: string) => + v.split("").reduce((acc, cur) => ((acc[cur] = (acc[cur] ?? 0) + 1), acc), {});