From 1ad5d218edc068027b17e64b80f2e1a076acdf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Fri, 22 Aug 2025 16:09:59 +0900 Subject: [PATCH 1/6] best time to buy and sell stock solution --- best-time-to-buy-and-sell-stock/hyer0705.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/hyer0705.ts diff --git a/best-time-to-buy-and-sell-stock/hyer0705.ts b/best-time-to-buy-and-sell-stock/hyer0705.ts new file mode 100644 index 000000000..98bb22149 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/hyer0705.ts @@ -0,0 +1,14 @@ +function maxProfit(prices: number[]): number { + let minPrice = Infinity; + let maxProfit = 0; + + for (const price of prices) { + if (price < minPrice) { + minPrice = price; + } else { + maxProfit = Math.max(maxProfit, price - minPrice); + } + } + + return maxProfit; +} From 635eff6960b6527d2be9f2bac564219f456763a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Fri, 22 Aug 2025 16:33:58 +0900 Subject: [PATCH 2/6] group anagrams solution --- group-anagrams/hyer0705.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 group-anagrams/hyer0705.ts diff --git a/group-anagrams/hyer0705.ts b/group-anagrams/hyer0705.ts new file mode 100644 index 000000000..bdd130842 --- /dev/null +++ b/group-anagrams/hyer0705.ts @@ -0,0 +1,15 @@ +function groupAnagrams(strs: string[]): string[][] { + const anagramsMap = new Map(); + + for (const word of strs) { + const anagramKey = [...word].sort().join(); + + if (anagramsMap.has(anagramKey)) { + anagramsMap.get(anagramKey)!.push(word); + } else { + anagramsMap.set(anagramKey, [word]); + } + } + + return Array.from(anagramsMap.values()); +} From 88c4d77aad1aa6cf8156bd2123fe229b1468227c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Fri, 22 Aug 2025 17:27:40 +0900 Subject: [PATCH 3/6] encode and decode strings solution --- encode-and-decode-strings/hyer0705.ts | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 encode-and-decode-strings/hyer0705.ts diff --git a/encode-and-decode-strings/hyer0705.ts b/encode-and-decode-strings/hyer0705.ts new file mode 100644 index 000000000..46a43c229 --- /dev/null +++ b/encode-and-decode-strings/hyer0705.ts @@ -0,0 +1,47 @@ +class Solution { + private DELIMITER = "*"; + + /** + * @param strs: a list of strings + * @returns: encodes a list of strings to a single string. + */ + public encode(strs: string[]): string { + let encoded = ""; + + for (const str of strs) { + encoded += `${str.length}${this.DELIMITER}${str}`; + } + + return encoded; + } + + /** + * @param str: A string + * @returns: decodes a single string to a list of strings + */ + public decode(str: string): string[] { + const decoded: string[] = []; + + let stack: string[] = []; + let pointer = 0; + + while (pointer < str.length) { + const char = str[pointer]; + + if (char === this.DELIMITER) { + let strLength: number = Number(stack.join("")); + stack = []; + + const word = str.substring(pointer + 1, pointer + 1 + strLength); + pointer = pointer + 1 + strLength; + + decoded.push(word); + } else { + stack.push(char); + pointer++; + } + } + + return decoded; + } +} From 050a6b280187bc9dd4ceb90096578655e0d77bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Fri, 22 Aug 2025 18:19:12 +0900 Subject: [PATCH 4/6] implement trie (prefix tree) solution --- implement-trie-prefix-tree/hyer0705.ts | 69 ++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 implement-trie-prefix-tree/hyer0705.ts diff --git a/implement-trie-prefix-tree/hyer0705.ts b/implement-trie-prefix-tree/hyer0705.ts new file mode 100644 index 000000000..16b3374c3 --- /dev/null +++ b/implement-trie-prefix-tree/hyer0705.ts @@ -0,0 +1,69 @@ +class TNode { + isEndOf: boolean; + children: Map; + + constructor() { + this.isEndOf = false; + this.children = new Map(); + } +} + +class Trie { + private root: TNode; + + constructor() { + this.root = new TNode(); + } + + insert(word: string): void { + let currentNode: TNode = this.root; + + for (const ch of word) { + if (currentNode.children.has(ch)) { + currentNode = currentNode.children.get(ch)!; + } else { + const newNode = new TNode(); + + currentNode.children.set(ch, newNode); + currentNode = currentNode.children.get(ch)!; + } + } + currentNode.isEndOf = true; + } + + search(word: string): boolean { + let currentNode = this.root; + + for (const ch of word) { + if (!currentNode.children.has(ch)) { + return false; + } + + currentNode = currentNode.children.get(ch)!; + } + + return currentNode.isEndOf; + } + + startsWith(prefix: string): boolean { + let currentNode = this.root; + + for (const ch of prefix) { + if (!currentNode.children.has(ch)) { + return false; + } + + currentNode = currentNode.children.get(ch)!; + } + + return true; + } +} + +/** + * Your Trie object will be instantiated and called as such: + * var obj = new Trie() + * obj.insert(word) + * var param_2 = obj.search(word) + * var param_3 = obj.startsWith(prefix) + */ From 064d73aa3068691e90d3ceb3966f9a9fa06bcfac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Fri, 22 Aug 2025 19:51:22 +0900 Subject: [PATCH 5/6] word break solution --- word-break/hyer0705.ts | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 word-break/hyer0705.ts diff --git a/word-break/hyer0705.ts b/word-break/hyer0705.ts new file mode 100644 index 000000000..691736c6b --- /dev/null +++ b/word-break/hyer0705.ts @@ -0,0 +1,71 @@ +class TNode { + isEndOf: boolean; + children: Map; + + constructor() { + this.isEndOf = false; + this.children = new Map(); + } +} + +class Trie { + root: TNode; + + constructor() { + this.root = new TNode(); + } + + insert(word: string): void { + let currentNode = this.root; + + for (const ch of word) { + if (!currentNode.children.has(ch)) { + currentNode.children.set(ch, new TNode()); + } + + currentNode = currentNode.children.get(ch)!; + } + + currentNode.isEndOf = true; + } + + search(word: string): boolean { + let currentNode = this.root; + + for (const ch of word) { + if (!currentNode.children.has(ch)) { + return false; + } + + currentNode = currentNode.children.get(ch)!; + } + + return currentNode.isEndOf; + } +} + +function wordBreak(s: string, wordDict: string[]): boolean { + const sLen = s.length; + + const dp: boolean[] = new Array(sLen + 1).fill(false); + dp[0] = true; + + const trie = new Trie(); + + for (const word of wordDict) { + trie.insert(word); + } + + for (let i = 1; i <= sLen; i++) { + for (let j = 0; j < i; j++) { + if (dp[j]) { + if (trie.search(s.substring(j, i))) { + dp[i] = true; + break; + } + } + } + } + + return dp[sLen]; +} From b5fc3ecfb5d5a19650891e798b9da50bbb987897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Fri, 22 Aug 2025 20:00:16 +0900 Subject: [PATCH 6/6] word break solution - using set --- word-break/hyer0705.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/word-break/hyer0705.ts b/word-break/hyer0705.ts index 691736c6b..9226a16f6 100644 --- a/word-break/hyer0705.ts +++ b/word-break/hyer0705.ts @@ -1,3 +1,25 @@ +// using set +function wordBreak(s: string, wordDict: string[]): boolean { + const sLen = s.length; + + const dp: boolean[] = new Array(sLen + 1).fill(false); + dp[0] = true; + + const wordSet = new Set(wordDict); + + for (let i = 1; i <= sLen; i++) { + for (let j = 0; j < i; j++) { + if (dp[j] && wordSet.has(s.substring(j, i))) { + dp[i] = true; + break; + } + } + } + + return dp[sLen]; +} + +// using trie class TNode { isEndOf: boolean; children: Map;