|
| 1 | +- https://leetcode.com/problems/design-add-and-search-words-data-structure |
| 2 | +- https://algorithm.jonghoonpark.com/2024/06/30/leetcode-211 |
| 3 | + |
| 4 | +## brute force |
| 5 | + |
| 6 | +์์ฌ์์ฌํ๊ฒ ํต๊ณผํ๋ค. |
| 7 | + |
| 8 | +```java |
| 9 | +class WordDictionary { |
| 10 | + |
| 11 | + Set<String> wordSet; |
| 12 | + |
| 13 | + public WordDictionary() { |
| 14 | + wordSet = new HashSet<>(); |
| 15 | + } |
| 16 | + |
| 17 | + public void addWord(String word) { |
| 18 | + wordSet.add(word); |
| 19 | + } |
| 20 | + |
| 21 | + public boolean search(String word) { |
| 22 | + Deque<String> queue = new ArrayDeque<>(); |
| 23 | + queue.push(word); |
| 24 | + |
| 25 | + while (queue.getFirst().contains(".")) { |
| 26 | + String _word = queue.removeFirst(); |
| 27 | + String pre = _word.substring(0, _word.indexOf(".")); |
| 28 | + String post = _word.substring(_word.indexOf(".") + 1); |
| 29 | + |
| 30 | + for (char c = 'a'; c <= 'z'; c++) { |
| 31 | + queue.addLast(pre + c + post); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + while (!queue.isEmpty()) { |
| 36 | + String _word = queue.removeFirst(); |
| 37 | + if (wordSet.contains(_word)) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return false; |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### TC, SC |
| 48 | + |
| 49 | +- `.` ์ด ์์ ๋ |
| 50 | + - ์๊ฐ ๋ณต์ก๋ : `O(1)` |
| 51 | + - ๊ณต๊ฐ ๋ณต์ก๋ : `O(1)` |
| 52 | +- `.` ์ด ์์ ๋ |
| 53 | + - ์๊ฐ ๋ณต์ก๋ : `O(26^N)` |
| 54 | + - ๊ณต๊ฐ ๋ณต์ก๋ : `O(26^N)` |
| 55 | + - ์ฌ๊ธฐ์ N์ `.` ์ ์ |
| 56 | + |
| 57 | +## trie |
| 58 | + |
| 59 | +[208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) ๋ฌธ์ ์์ ์ฌ์ฉํ Trie ์ฌ์ฌ์ฉ. |
| 60 | + |
| 61 | +```java |
| 62 | +class WordDictionary { |
| 63 | + |
| 64 | + Trie trie; // Trie ๊ตฌํ์ ์๋ต |
| 65 | + |
| 66 | + public WordDictionary() { |
| 67 | + trie = new Trie(); |
| 68 | + } |
| 69 | + |
| 70 | + public void addWord(String word) { |
| 71 | + trie.insert(word); |
| 72 | + } |
| 73 | + |
| 74 | + public boolean search(String word) { |
| 75 | + if (word.contains(".")) { |
| 76 | + String pre = word.substring(0, word.indexOf(".")); |
| 77 | + String post = word.substring(word.indexOf(".") + 1); |
| 78 | + |
| 79 | + if (trie.startsWith(pre)) { |
| 80 | + for (char c = 'a'; c <= 'z'; c++) { |
| 81 | + if (search(pre + c + post)) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + return trie.search(word); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### TC, SC |
| 98 | + |
| 99 | +์
๋ ฅ๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ `L`, `.` ์ ์๋ฅผ `N` ์ด๋ผ๊ณ ํ์์ ๋ |
| 100 | + |
| 101 | +addWord ๋ฉ์๋์ ๊ฒฝ์ฐ ์๊ฐ ๋ณต์ก๋๋ `O(L)`์ด๋ค. |
| 102 | +search ๋ฉ์๋์ ๊ฒฝ์ฐ ์
๋ ฅ๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ n ์ด๋ผ ํ์์ ๋ ์๊ฐ ๋ณต์ก๋๋ `O(L * 26 ^ N)`์ด๋ค. |
| 103 | + |
| 104 | +๊ณต๊ฐ ๋ณต์ก๋๋ Trie ๊ตฌ์กฐ๋ฅผ ๋ง๋๋๋ฐ ์ฌ์ฉ๋ ๊ณต๊ฐ์ด๋ค. `insert๋ ๋ฌธ์์ด ๊ธธ์ด์ ํ๊ท ` ๋ฅผ `avg(L)`์ด๋ผ๊ณ ํ์์ ๋ `O(avg(L) * 26)`์ด๋ค. 26์ ๊ณ์์ด๊ธฐ ๋๋ฌธ์ ์๋ตํ ์ ์๋ค. |
| 105 | + |
| 106 | +## trie ๊ฐ์ |
| 107 | + |
| 108 | +์ด ๋ฌธ์ ์ ์ ํฉํ๋๋ก search๋ฅผ ์์ ํ์๋ค. |
| 109 | + |
| 110 | +```java |
| 111 | +class WordDictionary { |
| 112 | + |
| 113 | + Trie trie; |
| 114 | + |
| 115 | + public WordDictionary() { |
| 116 | + trie = new Trie(); |
| 117 | + } |
| 118 | + |
| 119 | + public void addWord(String word) { |
| 120 | + trie.insert(word); |
| 121 | + } |
| 122 | + |
| 123 | + public boolean search(String word) { |
| 124 | + return trie.search(word); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +class Trie { |
| 131 | + |
| 132 | + Node root = new Node(); |
| 133 | + |
| 134 | + public Trie() { |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + public void insert(String word) { |
| 139 | + Node currentNode = root; |
| 140 | + for (char c : word.toCharArray()) { |
| 141 | + if (currentNode.nodes[c - 97] == null) { |
| 142 | + currentNode.nodes[c - 97] = new Node(); |
| 143 | + } |
| 144 | + currentNode = currentNode.nodes[c - 97]; |
| 145 | + } |
| 146 | + currentNode.val = word; |
| 147 | + } |
| 148 | + |
| 149 | + public boolean search(String word) { |
| 150 | + return search(root, word, 0); |
| 151 | + } |
| 152 | + |
| 153 | + public boolean search(Node node, String word, int index) { |
| 154 | + if (node == null) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + if (node.val != null && node.val.length() == word.length()) { |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + if (index >= word.length()) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + char c = word.charAt(index); |
| 167 | + |
| 168 | + if (c == '.') { |
| 169 | + for (char _c = 'a'; _c <= 'z'; _c++) { |
| 170 | + if (search(node.nodes[_c - 97], word, index + 1)) { |
| 171 | + return true; |
| 172 | + } |
| 173 | + } |
| 174 | + return false; |
| 175 | + } else if (node.nodes[c - 97] == null) { |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + return search(node.nodes[c - 97], word, index + 1); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +class Node { |
| 184 | + String val; |
| 185 | + Node[] nodes = new Node[26]; |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +### TC, SC |
| 190 | + |
| 191 | +์
๋ ฅ๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ `L`, `.` ์ ์๋ฅผ `N` ์ด๋ผ๊ณ ํ์์ ๋ |
| 192 | + |
| 193 | +addWord ๋ฉ์๋์ ๊ฒฝ์ฐ ์๊ฐ ๋ณต์ก๋๋ `O(L)`์ด๋ค. |
| 194 | +search ๋ฉ์๋์ ๊ฒฝ์ฐ ์
๋ ฅ๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ n ์ด๋ผ ํ์์ ๋ ์๊ฐ ๋ณต์ก๋๋ `O(L * 26 ^ N)`์ด๋ค. |
| 195 | +๊ฐ์ ์ ๊ณผ ๋น๊ตํด๋ดค์ ๋ ํ๊ธฐ์์ผ๋ก๋ ์ฐจ์ด๊ฐ ์์ผ๋, ๋ถํ์ํ ๊ณผ์ ์ ์ ๊ฑฐํ๊ฒ๋์ด์ ์๊ฐ์ด ๋งค์ฐ ๋จ์ถ๋๋ค. |
| 196 | +(`trie.startsWith(pre)`์ด ์ฌ๋ผ์ก๊ณ , search์ ํธ์ถ ํ์๊ฐ ์ค์ด๋ฌ.) |
| 197 | + |
| 198 | +๊ณต๊ฐ ๋ณต์ก๋๋ Trie ๊ตฌ์กฐ๋ฅผ ๋ง๋๋๋ฐ ์ฌ์ฉ๋ ๊ณต๊ฐ์ด๋ค. `insert๋ ๋ฌธ์์ด ๊ธธ์ด์ ํ๊ท ` ๋ฅผ `avg(L)`์ด๋ผ๊ณ ํ์์ ๋ `O(avg(L) * 26)`์ด๋ค. 26์ ๊ณ์์ด๊ธฐ ๋๋ฌธ์ ์๋ตํ ์ ์๋ค. |
0 commit comments