-
Notifications
You must be signed in to change notification settings - Fork 46
/
Trie2.cpp
58 lines (50 loc) · 1.59 KB
/
Trie2.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
47
48
49
50
51
52
53
54
55
56
57
58
class MagicDictionary {
public:
/** Initialize your data structure here. */
struct trie{
trie* next[27];
int end;
};
trie *root;
MagicDictionary() {
root = new trie();
}
/** Build a dictionary through a list of words */
void buildDict(vector<string> dict) {
for ( string x : dict){
trie* temp = root;
for ( char c : x){
if ( !temp -> next[c -'a'] ) temp->next[c-'a']= new trie();
temp = temp->next[c-'a'];
}
temp->end =1;
}
}
bool get (string word ,trie* temp , int index =0,int change = 1){
if ( change < 0 ) {
return false;
}
if ( index == word.size()) return ( change == 0 ) && temp->end;
bool found = false;
for ( int i = 0; i < 26 ; i ++ ) {
if ( temp -> next[i] ){
if ( i == word[index] -'a')
found |= get( word , temp->next[i] , index +1 , change);
else
found |= get( word , temp->next[i] , index +1, change -1);
}
}
return found;
}
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
bool search(string word) {
trie* temp = root;
return get( word , temp);
}
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary* obj = new MagicDictionary();
* obj->buildDict(dict);
* bool param_2 = obj->search(word);
*/