forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
46 lines (40 loc) · 1.09 KB
/
Solution.cpp
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
46
class MagicDictionary {
public:
/** Initialize your data structure here. */
MagicDictionary() {
}
void buildDict(vector<string> dictionary) {
for (string word : dictionary)
{
words.insert(word);
for (string p : patterns(word)) ++counter[p];
}
}
bool search(string searchWord) {
for (string p : patterns(searchWord))
{
if (counter[p] > 1 || (counter[p] == 1 && !words.count(searchWord))) return true;
}
return false;
}
private:
unordered_set<string> words;
unordered_map<string, int> counter;
vector<string> patterns(string word) {
vector<string> res;
for (int i = 0; i < word.size(); ++i)
{
char c = word[i];
word[i] = '*';
res.push_back(word);
word[i] = c;
}
return res;
}
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary* obj = new MagicDictionary();
* obj->buildDict(dictionary);
* bool param_2 = obj->search(searchWord);
*/