From 3475a97294546681d5432391d84327a0dad4f6a0 Mon Sep 17 00:00:00 2001 From: Eunice Hong Date: Wed, 16 Apr 2025 15:48:37 +0900 Subject: [PATCH 1/3] solve: Week 03 valid palindrome --- valid-palindrome/eunice-hong.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 valid-palindrome/eunice-hong.ts diff --git a/valid-palindrome/eunice-hong.ts b/valid-palindrome/eunice-hong.ts new file mode 100644 index 000000000..736a84505 --- /dev/null +++ b/valid-palindrome/eunice-hong.ts @@ -0,0 +1,30 @@ +function isPalindrome(s: string): boolean { + // 1. filter out non-alphanumeric characters + const validChars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnlopqrstuvwxyz0123456789"; + + // 2. compare the first and last characters + let i = 0; + let j = s.length - 1; + + while (i < j) { + const head = s[i]; + const tail = s[j]; + + if (!validChars.includes(head)) { + // 3. if the characters are not alphanumeric, move the pointer inward + i++; + } else if (!validChars.includes(tail)) { + // 3. if the characters are not alphanumeric, move the pointer inward + j--; + } else if (head.toLowerCase() !== tail.toLowerCase()) { + // 4. if the characters are not the same, return false + return false; + } else { + // 5. if the characters are the same, move the pointers inward + i++; + j--; + } + } + return true; +} From b2ce37e897bfb8d07e2276b93e9e62e682b57196 Mon Sep 17 00:00:00 2001 From: Eunice Hong Date: Wed, 16 Apr 2025 15:59:22 +0900 Subject: [PATCH 2/3] solve: Week 03 number of 1 bits --- number-of-1-bits/eunice-hong.ts | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 number-of-1-bits/eunice-hong.ts diff --git a/number-of-1-bits/eunice-hong.ts b/number-of-1-bits/eunice-hong.ts new file mode 100644 index 000000000..c3127d39c --- /dev/null +++ b/number-of-1-bits/eunice-hong.ts @@ -0,0 +1,7 @@ +function hammingWeight(n: number): number { + // 1. convert the number to a binary string + const binaryString = n.toString(2); + + // 2. count the number of 1s in the binary string + return binaryString.split("").filter((char) => char === "1").length; +} From 5e375a04ef1ddc5abc45648790016d3eee5cd374 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Fri, 18 Apr 2025 11:52:12 +0900 Subject: [PATCH 3/3] solve: Week 03 combination sum --- combination-sum/eunice-hong.ts | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 combination-sum/eunice-hong.ts diff --git a/combination-sum/eunice-hong.ts b/combination-sum/eunice-hong.ts new file mode 100644 index 000000000..c6fd2fe21 --- /dev/null +++ b/combination-sum/eunice-hong.ts @@ -0,0 +1,41 @@ +/** + * Finds all unique combinations of numbers from the candidates array that sum up to the target. + * + * @param candidates - An array of numbers. + * @param target - The target sum. + * @returns An array of arrays, each containing a unique combination of numbers that sum up to the target. + * + * Time Complexity: O(n^2) + * Space Complexity: O(n) + */ +function combinationSum(candidates: number[], target: number): number[][] { + // 1. Initialize result array + let combinations: number[][] = []; + let candidate: number[] = []; + + // 2. Define recursive function + function combination(start: number, total: number) { + console.log(`start:${start}, total:${total}`) + if (target < total) { + // 3. If total is greater than target, return + return; + } else if (target === total) { + // 4. If total is equal to target, push candidate to result + combinations.push([...candidate]) + return; + } else { + // 5. If total is less than target, iterate through candidates + for (let i = start; i < candidates.length; i++) { + // 6. Push candidate to result + candidate.push(candidates[i]) + // 7. Recursively call combination function + combination(i, total + candidates[i]) + // 8. Pop candidate from result + candidate.pop(); + } + } + } + combination(0, 0); + + return combinations; +};