-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementMagicDictionary.cpp
More file actions
45 lines (40 loc) · 1.2 KB
/
implementMagicDictionary.cpp
File metadata and controls
45 lines (40 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Source: https://leetcode.com/problems/implement-magic-dictionary/
// Author: Miao Zhang
// Date: 2021-02-28
class MagicDictionary {
public:
/** Initialize your data structure here. */
MagicDictionary() {
dict_.clear();
}
void buildDict(vector<string> dictionary) {
for (auto& word: dictionary) {
for (int i = 0; i < word.size(); i++) {
char c = word[i];
word[i] = '*';
dict_[word].insert(c);
word[i] = c;
}
}
}
bool search(string searchWord) {
for (int i = 0; i < searchWord.size(); i++) {
char c = searchWord[i];
searchWord[i] = '*';
if (dict_.count(searchWord)) {
auto& charset = dict_[searchWord];
if (!charset.count(c) || charset.size() > 1) return true;
}
searchWord[i] = c;
}
return false;
}
private:
unordered_map<string, unordered_set<char>> dict_;
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary* obj = new MagicDictionary();
* obj->buildDict(dictionary);
* bool param_2 = obj->search(searchWord);
*/