From e6007ae58d2f8188e107aaf9309b6710c89650fc Mon Sep 17 00:00:00 2001 From: tolluset Date: Sun, 25 Aug 2024 02:04:50 +0900 Subject: [PATCH 1/6] solve: valid anagram --- valid-anagram/tolluset.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 valid-anagram/tolluset.ts 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), {}); From 6547750b5b76bddc00a6a7bf9b8210e2a2a5b220 Mon Sep 17 00:00:00 2001 From: tolluset Date: Sun, 25 Aug 2024 02:42:58 +0900 Subject: [PATCH 2/6] solve: counting bits --- counting-bits/tolluset.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 counting-bits/tolluset.ts 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, + ); +} From bd504ffa3762b95d5e8f111293b9cf1e9fb4e69d Mon Sep 17 00:00:00 2001 From: tolluset Date: Sun, 25 Aug 2024 04:12:18 +0900 Subject: [PATCH 3/6] solve: encode and decode strings --- decode-ways/tolluset.ts | 0 encode-and-decode-strings/tolluset.ts | 7 +++++++ 2 files changed, 7 insertions(+) create mode 100644 decode-ways/tolluset.ts create mode 100644 encode-and-decode-strings/tolluset.ts diff --git a/decode-ways/tolluset.ts b/decode-ways/tolluset.ts new file mode 100644 index 000000000..e69de29bb diff --git a/encode-and-decode-strings/tolluset.ts b/encode-and-decode-strings/tolluset.ts new file mode 100644 index 000000000..c85088202 --- /dev/null +++ b/encode-and-decode-strings/tolluset.ts @@ -0,0 +1,7 @@ +function encode(arr: string[]): string { + return arr.join("🎃"); +} + +function decode(str: string): string[] { + return str.split("🎃"); +} From 4388ac369ea198af8706debc4d4c3f80e0977756 Mon Sep 17 00:00:00 2001 From: tolluset Date: Sun, 25 Aug 2024 11:20:47 +0900 Subject: [PATCH 4/6] chore: add complexity --- encode-and-decode-strings/tolluset.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/encode-and-decode-strings/tolluset.ts b/encode-and-decode-strings/tolluset.ts index c85088202..145170bbd 100644 --- a/encode-and-decode-strings/tolluset.ts +++ b/encode-and-decode-strings/tolluset.ts @@ -1,3 +1,7 @@ +/* + * TC: O(nm) + * SC: O(nm) + * */ function encode(arr: string[]): string { return arr.join("🎃"); } From 4ac54879ca2c697f04259a40c0f6e01098ec366f Mon Sep 17 00:00:00 2001 From: tolluset Date: Mon, 26 Aug 2024 03:45:54 +0900 Subject: [PATCH 5/6] solve: construct binary tree from preorder and inorder traversal --- .../tolluset.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/tolluset.ts 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; +} From 91f1443862ac6d64ceb55b6cc20456b5a63c7320 Mon Sep 17 00:00:00 2001 From: tolluset Date: Mon, 26 Aug 2024 04:11:41 +0900 Subject: [PATCH 6/6] solve: decode ways --- decode-ways/tolluset.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/decode-ways/tolluset.ts b/decode-ways/tolluset.ts index e69de29bb..eab623a82 100644 --- a/decode-ways/tolluset.ts +++ 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]; +}