|
67 | 67 |
|
68 | 68 | <p><meta charset="UTF-8" />注意:本题与主站 648 题相同: <a href="https://leetcode-cn.com/problems/replace-words/">https://leetcode-cn.com/problems/replace-words/</a></p>
|
69 | 69 |
|
70 |
| - |
71 | 70 | ## 解法
|
72 | 71 |
|
73 | 72 | <!-- 这里可写通用的实现逻辑 -->
|
74 | 73 |
|
| 74 | +前缀树实现。 |
| 75 | + |
75 | 76 | <!-- tabs:start -->
|
76 | 77 |
|
77 | 78 | ### **Python3**
|
78 | 79 |
|
79 | 80 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
80 | 81 |
|
81 | 82 | ```python
|
82 |
| - |
| 83 | +class Trie: |
| 84 | + def __init__(self) -> None: |
| 85 | + self.children = [None] * 26 |
| 86 | + self.root = None |
| 87 | + |
| 88 | + |
| 89 | +class Solution: |
| 90 | + def replaceWords(self, dictionary: List[str], sentence: str) -> str: |
| 91 | + trie = Trie() |
| 92 | + for root in dictionary: |
| 93 | + cur = trie |
| 94 | + for c in root: |
| 95 | + idx = ord(c) - ord('a') |
| 96 | + if cur.children[idx] is None: |
| 97 | + cur.children[idx] = Trie() |
| 98 | + cur = cur.children[idx] |
| 99 | + cur.root = root |
| 100 | + |
| 101 | + ans = [] |
| 102 | + for word in sentence.split(): |
| 103 | + cur = trie |
| 104 | + for c in word: |
| 105 | + idx = ord(c) - ord('a') |
| 106 | + if cur.children[idx] is None or cur.root is not None: |
| 107 | + break |
| 108 | + cur = cur.children[idx] |
| 109 | + ans.append(word if cur.root is None else cur.root) |
| 110 | + return ' '.join(ans) |
83 | 111 | ```
|
84 | 112 |
|
85 | 113 | ### **Java**
|
86 | 114 |
|
87 | 115 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
88 | 116 |
|
89 | 117 | ```java
|
| 118 | +class Trie { |
| 119 | + Trie[] children = new Trie[26]; |
| 120 | + String root; |
| 121 | +} |
| 122 | + |
| 123 | +class Solution { |
| 124 | + public String replaceWords(List<String> dictionary, String sentence) { |
| 125 | + Trie trie = new Trie(); |
| 126 | + for (String root : dictionary) { |
| 127 | + Trie cur = trie; |
| 128 | + for (char c : root.toCharArray()) { |
| 129 | + if (cur.children[c - 'a'] == null) { |
| 130 | + cur.children[c - 'a'] = new Trie(); |
| 131 | + } |
| 132 | + cur = cur.children[c - 'a']; |
| 133 | + } |
| 134 | + cur.root = root; |
| 135 | + } |
| 136 | + List<String> ans = new ArrayList<>(); |
| 137 | + for (String word : sentence.split("\\s+")) { |
| 138 | + Trie cur = trie; |
| 139 | + for (char c : word.toCharArray()) { |
| 140 | + if (cur.children[c - 'a'] == null || cur.root != null) { |
| 141 | + break; |
| 142 | + } |
| 143 | + cur = cur.children[c - 'a']; |
| 144 | + } |
| 145 | + ans.add(cur.root == null ? word : cur.root); |
| 146 | + } |
| 147 | + return String.join(" ", ans); |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### **C++** |
| 153 | + |
| 154 | +```cpp |
| 155 | +class Trie { |
| 156 | +public: |
| 157 | + string root; |
| 158 | + vector<Trie*> children; |
| 159 | + |
| 160 | + Trie() { |
| 161 | + root = ""; |
| 162 | + children.resize(26); |
| 163 | + } |
| 164 | +}; |
| 165 | + |
| 166 | +class Solution { |
| 167 | +public: |
| 168 | + string replaceWords(vector<string>& dictionary, string sentence) { |
| 169 | + Trie* trie = new Trie(); |
| 170 | + for (auto root : dictionary) |
| 171 | + { |
| 172 | + Trie* cur = trie; |
| 173 | + for (char c : root) |
| 174 | + { |
| 175 | + if (!cur->children[c - 'a']) cur->children[c - 'a'] = new Trie(); |
| 176 | + cur = cur->children[c - 'a']; |
| 177 | + } |
| 178 | + cur->root = root; |
| 179 | + } |
| 180 | + |
| 181 | + string ans = ""; |
| 182 | + istringstream is(sentence); |
| 183 | + vector<string> ss; |
| 184 | + string s; |
| 185 | + while (is >> s) ss.push_back(s); |
| 186 | + for (auto word : ss) |
| 187 | + { |
| 188 | + Trie* cur = trie; |
| 189 | + for (char c : word) |
| 190 | + { |
| 191 | + if (!cur->children[c - 'a'] || cur->root != "") break; |
| 192 | + cur = cur->children[c - 'a']; |
| 193 | + } |
| 194 | + ans += cur->root == "" ? word : cur->root; |
| 195 | + ans += " "; |
| 196 | + } |
| 197 | + ans.pop_back(); |
| 198 | + return ans; |
| 199 | + } |
| 200 | +}; |
| 201 | +``` |
90 | 202 |
|
| 203 | +### **Go** |
| 204 | + |
| 205 | +```go |
| 206 | +func replaceWords(dictionary []string, sentence string) string { |
| 207 | + trie := &Trie{} |
| 208 | + for _, root := range dictionary { |
| 209 | + cur := trie |
| 210 | + for _, c := range root { |
| 211 | + c -= 'a' |
| 212 | + if cur.children[c] == nil { |
| 213 | + cur.children[c] = &Trie{} |
| 214 | + } |
| 215 | + cur = cur.children[c] |
| 216 | + } |
| 217 | + cur.root = root |
| 218 | + } |
| 219 | + |
| 220 | + var ans []string |
| 221 | + for _, word := range strings.Split(sentence, " ") { |
| 222 | + cur := trie |
| 223 | + for _, c := range word { |
| 224 | + c -= 'a' |
| 225 | + if cur.children[c] == nil || cur.root != "" { |
| 226 | + break |
| 227 | + } |
| 228 | + cur = cur.children[c] |
| 229 | + } |
| 230 | + if cur.root == "" { |
| 231 | + ans = append(ans, word) |
| 232 | + } else { |
| 233 | + ans = append(ans, cur.root) |
| 234 | + } |
| 235 | + } |
| 236 | + return strings.Join(ans, " ") |
| 237 | +} |
| 238 | + |
| 239 | +type Trie struct { |
| 240 | + children [26]*Trie |
| 241 | + root string |
| 242 | +} |
91 | 243 | ```
|
92 | 244 |
|
93 | 245 | ### **...**
|
|
0 commit comments