@@ -64,22 +64,197 @@ magicDictionary.search("leetcoded"); // 返回 False
64
64
65
65
<!-- 这里可写通用的实现逻辑 -->
66
66
67
+ 哈希表实现。
68
+
67
69
<!-- tabs:start -->
68
70
69
71
### ** Python3**
70
72
71
73
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
74
73
75
``` python
76
+ class MagicDictionary :
77
+
78
+ def __init__ (self ):
79
+ """
80
+ Initialize your data structure here.
81
+ """
82
+
83
+ def _patterns (self , word ):
84
+ return [word[:i] + ' *' + word[i + 1 :] for i in range (len (word))]
85
+
86
+ def buildDict (self , dictionary : List[str ]) -> None :
87
+ self .words = set (dictionary)
88
+ self .counter = collections.Counter(
89
+ p for word in dictionary for p in self ._patterns(word))
90
+
91
+ def search (self , searchWord : str ) -> bool :
92
+ for p in self ._patterns(searchWord):
93
+ if self .counter[p] > 1 or (self .counter[p] == 1 and searchWord not in self .words):
94
+ return True
95
+ return False
96
+
74
97
98
+ # Your MagicDictionary object will be instantiated and called as such:
99
+ # obj = MagicDictionary()
100
+ # obj.buildDict(dictionary)
101
+ # param_2 = obj.search(searchWord)
75
102
```
76
103
77
104
### ** Java**
78
105
79
106
<!-- 这里可写当前语言的特殊实现逻辑 -->
80
107
81
108
``` java
109
+ class MagicDictionary {
110
+ private Set<String > words;
111
+ private Map<String , Integer > counter;
112
+
113
+ /* * Initialize your data structure here. */
114
+ public MagicDictionary () {
115
+ words = new HashSet<> ();
116
+ counter = new HashMap<> ();
117
+ }
118
+
119
+ public void buildDict (String [] dictionary ) {
120
+ for (String word : dictionary) {
121
+ words. add(word);
122
+ for (String p : patterns(word)) {
123
+ counter. put(p, counter. getOrDefault(p, 0 ) + 1 );
124
+ }
125
+ }
126
+ }
127
+
128
+ public boolean search (String searchWord ) {
129
+ for (String p : patterns(searchWord)) {
130
+ int cnt = counter. getOrDefault(p, 0 );
131
+ if (cnt > 1 || (cnt == 1 && ! words. contains(searchWord))) {
132
+ return true ;
133
+ }
134
+ }
135
+ return false ;
136
+ }
137
+
138
+ private List<String > patterns (String word ) {
139
+ List<String > res = new ArrayList<> ();
140
+ char [] chars = word. toCharArray();
141
+ for (int i = 0 ; i < chars. length; ++ i) {
142
+ char c = chars[i];
143
+ chars[i] = ' *' ;
144
+ res. add(new String (chars));
145
+ chars[i] = c;
146
+ }
147
+ return res;
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Your MagicDictionary object will be instantiated and called as such:
153
+ * MagicDictionary obj = new MagicDictionary();
154
+ * obj.buildDict(dictionary);
155
+ * boolean param_2 = obj.search(searchWord);
156
+ */
157
+ ```
158
+
159
+ ### ** C++**
160
+
161
+ ``` cpp
162
+ class MagicDictionary {
163
+ public:
164
+ /** Initialize your data structure here. * /
165
+ MagicDictionary() {
166
+
167
+ }
168
+
169
+ void buildDict (vector<string > dictionary) {
170
+ for (string word : dictionary)
171
+ {
172
+ words.insert(word);
173
+ for (string p : patterns(word)) ++counter[ p] ;
174
+ }
175
+ }
176
+
177
+ bool search(string searchWord) {
178
+ for (string p : patterns(searchWord))
179
+ {
180
+ if (counter[ p] > 1 || (counter[ p] == 1 && !words.count(searchWord))) return true;
181
+ }
182
+ return false;
183
+ }
184
+
185
+ private:
186
+ unordered_set<string > words;
187
+ unordered_map<string, int> counter;
188
+
189
+ vector<string> patterns(string word) {
190
+ vector<string> res;
191
+ for (int i = 0; i < word.size(); ++i)
192
+ {
193
+ char c = word[i];
194
+ word[i] = '*';
195
+ res.push_back(word);
196
+ word[i] = c;
197
+ }
198
+ return res;
199
+ }
200
+ };
201
+
202
+ /**
203
+ * Your MagicDictionary object will be instantiated and called as such:
204
+ * MagicDictionary* obj = new MagicDictionary();
205
+ * obj->buildDict(dictionary);
206
+ * bool param_2 = obj->search(searchWord);
207
+ * /
208
+ ```
209
+
210
+ ### **Go**
211
+
212
+ ```go
213
+ type MagicDictionary struct {
214
+ words map[string]bool
215
+ counter map[string]int
216
+ }
217
+
218
+ /** Initialize your data structure here. */
219
+ func Constructor() MagicDictionary {
220
+ return MagicDictionary{
221
+ words: make(map[string]bool),
222
+ counter: make(map[string]int),
223
+ }
224
+ }
225
+
226
+ func (this *MagicDictionary) BuildDict(dictionary []string) {
227
+ for _, word := range dictionary {
228
+ this.words[word] = true
229
+ for _, p := range patterns(word) {
230
+ this.counter[p]++
231
+ }
232
+ }
233
+ }
234
+
235
+ func (this *MagicDictionary) Search(searchWord string) bool {
236
+ for _, p := range patterns(searchWord) {
237
+ if this.counter[p] > 1 || (this.counter[p] == 1 && !this.words[searchWord]) {
238
+ return true
239
+ }
240
+ }
241
+ return false
242
+ }
243
+
244
+ func patterns(word string) []string {
245
+ var res []string
246
+ for i := 0; i < len(word); i++ {
247
+ res = append(res, word[:i]+"."+word[i+1:])
248
+ }
249
+ return res
250
+ }
82
251
252
+ /**
253
+ * Your MagicDictionary object will be instantiated and called as such:
254
+ * obj := Constructor();
255
+ * obj.BuildDict(dictionary);
256
+ * param_2 := obj.Search(searchWord);
257
+ */
83
258
```
84
259
85
260
### ** ...**
0 commit comments