|
| 1 | +# μ°κ΄ λ§ν¬ |
| 2 | +- [λ¬Έμ νμ΄ μ€μΌμ€](https://github.com/orgs/DaleStudy/projects/6/views/5) |
| 3 | +- [λ΅μ μ½λ μ μΆλ²](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C) |
| 4 | + |
| 5 | +# Problem |
| 6 | +- λ¬Έμ λ§ν¬ : https://leetcode.com/problems/design-add-and-search-words-data-structure/description/ |
| 7 | +- λ¬Έμ μ΄λ¦ : design add and search words data structure |
| 8 | +- λ¬Έμ λ²νΈ : 211 |
| 9 | +- λμ΄λ : medium |
| 10 | +- μΉ΄ν
κ³ λ¦¬ : |
| 11 | + |
| 12 | +# λ¬Έμ μ€λͺ
|
| 13 | +- λ°μ΄ν°λ² μ΄μ€μ input wordλ₯Ό λνκ³ μ°Ύλ μλ£κ΅¬μ‘° λμμΈνκΈ° |
| 14 | + |
| 15 | +# μμ΄λμ΄ |
| 16 | +- μ΄μ μ£Όμ°¨ word searchμ λμΌν μ κ·Ό λ°©λ²μΌλ‘ νμ΄ κ°λ₯ |
| 17 | +### Trie |
| 18 | +```cpp |
| 19 | +class TrieNode{ |
| 20 | +public: |
| 21 | + bool isCompleteWord; |
| 22 | + TrieNode* children[26]; |
| 23 | + |
| 24 | + TrieNode() { |
| 25 | + isCompleteWord = false; |
| 26 | + memset(children, 0, sizeof(children)); |
| 27 | + } |
| 28 | +}; |
| 29 | +``` |
| 30 | + |
| 31 | +- κ°μ§ μ€κ°μΈμ§, μλλ©΄ μκ²°λ λ¨μ΄μΈμ§λ₯Ό νμΈν μ μμ. |
| 32 | +- ν΄λΉ λ‘μ§μ ν΅ν΄, Prefix νμΈνλ λ©μλ λν μ½κ² λ§λ€ μ μμ. |
| 33 | + |
| 34 | +# β
μ½λ (Solution) |
| 35 | + |
| 36 | +## Trie - wrong answer |
| 37 | +```cpp |
| 38 | +class TrieNode{ |
| 39 | +public: |
| 40 | + bool isCompleteWord; |
| 41 | + TrieNode* children[26]; |
| 42 | + |
| 43 | + TrieNode() { |
| 44 | + isCompleteWord = false; |
| 45 | + memset(children, 0, sizeof(children)); |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +class WordDictionary { |
| 50 | + TrieNode* root; |
| 51 | +public: |
| 52 | + WordDictionary() { |
| 53 | + root = new TrieNode(); |
| 54 | + } |
| 55 | + |
| 56 | + void addWord(string word) { |
| 57 | + auto cur = root; |
| 58 | + for(auto c: word){ |
| 59 | + if(!cur->children[c-'a']){ |
| 60 | + cur->children[c-'a'] = new TrieNode(); |
| 61 | + } |
| 62 | + cur = cur->children[c-'a']; |
| 63 | + } |
| 64 | + cur->isCompleteWord = true; |
| 65 | + } |
| 66 | + |
| 67 | + bool search(string word) { |
| 68 | + auto cur = root; |
| 69 | + for(auto c: word){ |
| 70 | + if(!cur->children[c-'a']){ |
| 71 | + return false; |
| 72 | + } |
| 73 | + cur = cur->children[c-'a']; |
| 74 | + } |
| 75 | + return cur->isCompleteWord; |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Your WordDictionary object will be instantiated and called as such: |
| 81 | + * WordDictionary* obj = new WordDictionary(); |
| 82 | + * obj->addWord(word); |
| 83 | + * bool param_2 = obj->search(word); |
| 84 | + */ |
| 85 | +``` |
| 86 | +
|
| 87 | +- μλͺ» μμ±ν μ½λ |
| 88 | +- μλ£κ΅¬μ‘° μ μ© μ€, edge case(`.` - dot)μ κ³ λ €νμ§ μμ |
| 89 | + - μλ λ¬Έμ λΌκ³ μ½κ² μκ°ν κ²μ΄ μλͺ». |
| 90 | +
|
| 91 | +```cpp |
| 92 | +class TrieNode { |
| 93 | +public: |
| 94 | + bool isCompleteWord; |
| 95 | + TrieNode* children[26]; |
| 96 | +
|
| 97 | + TrieNode() { |
| 98 | + isCompleteWord = false; |
| 99 | + memset(children, 0, sizeof(children)); |
| 100 | + } |
| 101 | +}; |
| 102 | +
|
| 103 | +class WordDictionary { |
| 104 | + TrieNode* root; |
| 105 | +
|
| 106 | + bool dfs(TrieNode* node, const string& word, int index) { |
| 107 | + if (!node) return false; |
| 108 | + if (index == word.size()) return node->isCompleteWord; |
| 109 | +
|
| 110 | + char c = word[index]; |
| 111 | + if (c == '.') { |
| 112 | + // '.'μ΄λ―λ‘ λͺ¨λ μμμ μλν΄λ³Έλ€ |
| 113 | + for (int i = 0; i < 26; ++i) { |
| 114 | + if (dfs(node->children[i], word, index + 1)) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | + } else { |
| 120 | + return dfs(node->children[c - 'a'], word, index + 1); |
| 121 | + } |
| 122 | + } |
| 123 | +
|
| 124 | +public: |
| 125 | + WordDictionary() { |
| 126 | + root = new TrieNode(); |
| 127 | + } |
| 128 | +
|
| 129 | + void addWord(string word) { |
| 130 | + TrieNode* cur = root; |
| 131 | + for (char c : word) { |
| 132 | + int idx = c - 'a'; |
| 133 | + if (!cur->children[idx]) { |
| 134 | + cur->children[idx] = new TrieNode(); |
| 135 | + } |
| 136 | + cur = cur->children[idx]; |
| 137 | + } |
| 138 | + cur->isCompleteWord = true; |
| 139 | + } |
| 140 | +
|
| 141 | + bool search(string word) { |
| 142 | + return dfs(root, word, 0); |
| 143 | + } |
| 144 | +}; |
| 145 | +
|
| 146 | +``` |
| 147 | + |
| 148 | +- dfsλ‘ κ΅¬ν |
| 149 | + |
| 150 | +# π μ½λ μ€λͺ
|
| 151 | + |
| 152 | + |
| 153 | +# μ΅μ ν ν¬μΈνΈ (Optimality Discussion) |
| 154 | +β’ μ΅μ νν μ΄μ μ μ리 |
| 155 | +β’ λ μ€μΌ μ μλ μ¬μ§λ μλκ°? |
| 156 | +β’ κΈ°μ‘΄ λ°©λ² λλΉ μΌλ§λ ν¨μ¨μ μ΄μλμ§ |
| 157 | + |
| 158 | +# π§ͺ ν
μ€νΈ & μ£μ§ μΌμ΄μ€ |
| 159 | + |
| 160 | +# π κ΄λ ¨ μ§μ λ³΅μ΅ |
| 161 | + |
| 162 | +# π νκ³ |
| 163 | + |
| 164 | + |
0 commit comments