diff --git a/solution/0600-0699/0648.Replace Words/README.md b/solution/0600-0699/0648.Replace Words/README.md index 457845d77a9cc..bf4509d821928 100644 --- a/solution/0600-0699/0648.Replace Words/README.md +++ b/solution/0600-0699/0648.Replace Words/README.md @@ -376,6 +376,49 @@ class Solution { } ``` +#### TypeScript + +```ts +function replaceWords(dictionary: string[], sentence: string): string { + const words = sentence.split(' '); + const trie: Trie = {}; + const TERMINAL_MARK = 'TERMINAL_MARK'; + + for (const s of dictionary) { + let t = trie; + + for (const ch of s) { + t[ch] ??= {}; + t = t[ch] as Trie_; + } + t[TERMINAL_MARK] = TERMINAL_MARK; + } + + for (let i = 0; i < words.length; i++) { + const s = words[i]; + let t = trie; + + for (let j = 0; j < s.length; j++) { + const ch = s[j]; + + if (!t[ch]) break; + + if ((t[ch] as Trie_)[TERMINAL_MARK]) { + words[i] = s.slice(0, j + 1); + break; + } + t = t[ch] as Trie_; + } + } + + return words.join(' '); +} + +// prettier-ignore +type Trie = { [key: string]: Trie} | string +type Trie_ = Exclude; +``` + diff --git a/solution/0600-0699/0648.Replace Words/README_EN.md b/solution/0600-0699/0648.Replace Words/README_EN.md index d55cb01f50ab2..09740702d1f62 100644 --- a/solution/0600-0699/0648.Replace Words/README_EN.md +++ b/solution/0600-0699/0648.Replace Words/README_EN.md @@ -363,6 +363,49 @@ class Solution { } ``` +#### TypeScript + +```ts +function replaceWords(dictionary: string[], sentence: string): string { + const words = sentence.split(' '); + const trie: Trie = {}; + const TERMINAL_MARK = 'TERMINAL_MARK'; + + for (const s of dictionary) { + let t = trie; + + for (const ch of s) { + t[ch] ??= {}; + t = t[ch] as Trie_; + } + t[TERMINAL_MARK] = TERMINAL_MARK; + } + + for (let i = 0; i < words.length; i++) { + const s = words[i]; + let t = trie; + + for (let j = 0; j < s.length; j++) { + const ch = s[j]; + + if (!t[ch]) break; + + if ((t[ch] as Trie_)[TERMINAL_MARK]) { + words[i] = s.slice(0, j + 1); + break; + } + t = t[ch] as Trie_; + } + } + + return words.join(' '); +} + +// prettier-ignore +type Trie = { [key: string]: Trie} | string +type Trie_ = Exclude; +``` + diff --git a/solution/0600-0699/0648.Replace Words/Solution2.ts b/solution/0600-0699/0648.Replace Words/Solution2.ts new file mode 100644 index 0000000000000..431b8689b426b --- /dev/null +++ b/solution/0600-0699/0648.Replace Words/Solution2.ts @@ -0,0 +1,38 @@ +function replaceWords(dictionary: string[], sentence: string): string { + const words = sentence.split(' '); + const trie: Trie = {}; + const TERMINAL_MARK = 'TERMINAL_MARK'; + + for (const s of dictionary) { + let t = trie; + + for (const ch of s) { + t[ch] ??= {}; + t = t[ch] as Trie_; + } + t[TERMINAL_MARK] = TERMINAL_MARK; + } + + for (let i = 0; i < words.length; i++) { + const s = words[i]; + let t = trie; + + for (let j = 0; j < s.length; j++) { + const ch = s[j]; + + if (!t[ch]) break; + + if ((t[ch] as Trie_)[TERMINAL_MARK]) { + words[i] = s.slice(0, j + 1); + break; + } + t = t[ch] as Trie_; + } + } + + return words.join(' '); +} + +// prettier-ignore +type Trie = { [key: string]: Trie} | string +type Trie_ = Exclude;