-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimplement-magic-dictionary.cpp
More file actions
41 lines (33 loc) · 989 Bytes
/
Copy pathimplement-magic-dictionary.cpp
File metadata and controls
41 lines (33 loc) · 989 Bytes
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
class MagicDictionary {
public:
/** Initialize your data structure here. */
MagicDictionary() {
}
/** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
m = dict;
}
int charDiff(string a, string b){
int diff = 0;
for(int i = 0; i < a.size(); i++){
if(a[i] != b[i]) diff++;
}
return diff;
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool search(string word) {
for(auto w : m){
if(word.size() != w.size()) continue;
if(charDiff(word, w) == 1) return true;
}
return false;
}
private:
vector<string> m;
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dict);
* bool param_2 = obj.search(word);
*/