|
| 1 | +/** |
| 2 | + *@link https://leetcode.com/problems/design-add-and-search-words-data-structure/ |
| 3 | + * |
| 4 | + * ์ ๊ทผ ๋ฐฉ๋ฒ : |
| 5 | + * - set ํ์ฉํ๋ ๋ฐฉ๋ฒ |
| 6 | + * - ๋จ์ด ์ถ๊ฐํ ๋๋ set์ ์ ์ฅํ๊ณ , ๋จ์ด ์ฐพ์ ๋๋ง๋ค cache๋ชฉ๋ก์์ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ. |
| 7 | + * - ๋จ์ด ์ถ๊ฐํ๋ฉด ์ด์ ์บ์๋ ์ฌ์ฉํ์ง ๋ชปํ๋๊น ํด๋ฆฌ์ด ํด์ฃผ๊ธฐ |
| 8 | + * - ์ด์ ๊ฐ ํ์ฉํ๋ค๋ ์ฅ์ ์ด ์์ง๋ง ๋จ์ด ์ถ๊ฐ์ ๋ค์ ์บ์ ๋ง๋ค์ด์ผํ๋ ๋นํจ์จ ์กด์ฌ |
| 9 | + * |
| 10 | + * ์๊ฐ๋ณต์ก๋ : O(n * k) |
| 11 | + * - n์ set์ ์ถ๊ฐ๋ ๋จ์ด ๊ธธ์ด, k๋ ๊ฐ ๋จ์ด ํ๊ท ๊ธธ์ด |
| 12 | + * - this.set ๋จ์ด ์ํ => O(n) |
| 13 | + * - replaceAll, filter => O(k) |
| 14 | + * |
| 15 | + * ๊ณต๊ฐ๋ณต์ก๋ : O(n * k) |
| 16 | + * - n์ ํจํด์ ๊ฐ์, k๋ ํจํด์ ๋งค์นญ๋๋ ํ๊ท ๋จ์ด ์ |
| 17 | + */ |
| 18 | +class WordDictionary { |
| 19 | + set: Set<string>; |
| 20 | + cache: Map<string, string[]>; |
| 21 | + constructor() { |
| 22 | + this.set = new Set(); |
| 23 | + this.cache = new Map(); |
| 24 | + } |
| 25 | + |
| 26 | + addWord(word: string): void { |
| 27 | + this.set.add(word); |
| 28 | + // ์๋ก์ด ๋จ์ด๊ฐ ์ถ๊ฐ ์ ์บ์ ํด๋ฆฌ์ด |
| 29 | + this.cache.clear(); |
| 30 | + } |
| 31 | + |
| 32 | + search(word: string): boolean { |
| 33 | + // ์บ์์ ๊ฐ์ด ์กด์ฌํ๋ฉด ์ฆ์ ๋ฆฌํด |
| 34 | + if (this.cache.has(word)) return this.cache.get(word)!.length > 0; |
| 35 | + |
| 36 | + // ์ ๊ทํํ์ ํจํด ์์ฑ |
| 37 | + const pattern = new RegExp(`^${word.replaceAll(".", "[a-z]")}$`); |
| 38 | + const matches = [...this.set].filter((item) => pattern.test(item)); |
| 39 | + |
| 40 | + // ๊ฒ์ ๊ฒฐ๊ณผ ์บ์์ ์ ์ฅ |
| 41 | + this.cache.set(word, matches); |
| 42 | + |
| 43 | + return matches.length > 0; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Your WordDictionary object will be instantiated and called as such: |
| 49 | + * var obj = new WordDictionary() |
| 50 | + * obj.addWord(word) |
| 51 | + * var param_2 = obj.search(word) |
| 52 | + */ |
| 53 | + |
| 54 | +// ์๊ฐ ๋ณต์ก๋ ๊ฐ์ ํ๊ธฐ ์ํด์ Trie ์๋ฃ ๊ตฌ์กฐ ์ฌ์ฉ |
| 55 | +class TrieNode { |
| 56 | + children: Map<string, TrieNode>; |
| 57 | + isEndOfWord: boolean; |
| 58 | + |
| 59 | + constructor() { |
| 60 | + this.children = new Map(); |
| 61 | + this.isEndOfWord = false; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/* |
| 66 | + * ์ ๊ทผ ๋ฐฉ๋ฒ : |
| 67 | + * - ๊ฐ ๋ฌธ์ ์ ์ฅํ๊ณ ๊ฒ์ํ๊ธฐ ์ํด์ Trie ๊ตฌ์กฐ ์ฌ์ฉ |
| 68 | + * - ์์ผ๋์นด๋(.)๊ฐ ํฌํจ๋ ๋จ์ด๋ ๋ชจ๋ ์์ ๋
ธ๋ ํ์ํ๊ธฐ ์ํด์ ์ฌ๊ท์ ์ผ๋ก ํ์ธ |
| 69 | + * |
| 70 | + * ์๊ฐ๋ณต์ก๋ : O(n * k) |
| 71 | + * - n : ๋จ์ด ๊ฐ์, m : ๋จ์ด ํ๊ท ๊ธธ์ด => addWord์ ๋ณต์ก๋ : O(n * m) |
| 72 | + * - c : ์์๋
ธ๋ ๊ฐ์, d : ์์ผ๋์นด๋ ๊ฐ์, m : ๋จ์ด ๊ธธ์ด => search์ ๋ณต์ก๋ : O(c^d * m) |
| 73 | + * - ์์ผ๋ ์นด๋ ์๋ ๊ฒฝ์ฐ ๊ฐ ๋
ธ๋ ๋ณ ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํด์ผ ํ๋ค. |
| 74 | + * |
| 75 | + * ๊ณต๊ฐ๋ณต์ก๋ : O(n * k) |
| 76 | + * - n์ ํจํด์ ๊ฐ์, k๋ ํจํด์ ๋งค์นญ๋๋ ํ๊ท ๋จ์ด ์ |
| 77 | + */ |
| 78 | +class WordDictionary { |
| 79 | + root: TrieNode; |
| 80 | + constructor() { |
| 81 | + this.root = new TrieNode(); |
| 82 | + } |
| 83 | + |
| 84 | + addWord(word: string): void { |
| 85 | + let currentNode = this.root; |
| 86 | + |
| 87 | + for (const letter of word) { |
| 88 | + if (!currentNode.children.has(letter)) |
| 89 | + currentNode.children.set(letter, new TrieNode()); |
| 90 | + currentNode = currentNode.children.get(letter)!; |
| 91 | + } |
| 92 | + |
| 93 | + currentNode.isEndOfWord = true; |
| 94 | + } |
| 95 | + |
| 96 | + search(word: string): boolean { |
| 97 | + const dfs = (currentNode: TrieNode, index: number): boolean => { |
| 98 | + if (index === word.length) return currentNode.isEndOfWord; |
| 99 | + |
| 100 | + const char = word[index]; |
| 101 | + // ์์ผ๋ ์นด๋์ธ ๊ฒฝ์ฐ ๋ชจ๋ ์์ ๋
ธ๋ ์ํํ๋ค |
| 102 | + if (char === ".") { |
| 103 | + // ๋ชจ๋ ์์ ๋
ธ๋ ์ํ |
| 104 | + for (const key of currentNode.children.keys()) { |
| 105 | + if (dfs(currentNode.children.get(key)!, index + 1)) return true; |
| 106 | + } |
| 107 | + return false; |
| 108 | + } else { |
| 109 | + if (!currentNode.children.has(char)) return false; |
| 110 | + return dfs(currentNode.children.get(char)!, index + 1); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + return dfs(this.root, 0); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Your WordDictionary object will be instantiated and called as such: |
| 120 | + * var obj = new WordDictionary() |
| 121 | + * obj.addWord(word) |
| 122 | + * var param_2 = obj.search(word) |
| 123 | + */ |
0 commit comments