-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path211_WordDictionary.cpp
More file actions
54 lines (51 loc) · 1.52 KB
/
211_WordDictionary.cpp
File metadata and controls
54 lines (51 loc) · 1.52 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
46
47
48
49
50
51
52
53
54
class WordDictionary {
public:
WordDictionary *node[26];
bool isEnd = false;
/** Initialize your data structure here. */
WordDictionary() {
for(int i = 0;i<26;i++)
node[i] = NULL;
isEnd = false;
}
/** Adds a word into the data structure. */
void addWord(string word) {
WordDictionary *now = this;
for(int i = 0;i<word.size();i++){
int ind = word[i]-'a';
if(now->node[ind]==NULL)
now->node[ind] = new WordDictionary();
now = now->node[ind];
}
now->isEnd = true;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool search(string word) {
return dfs(0,word,this);
}
bool dfs(int i,string word,WordDictionary *now){
if(i == word.size()){
return now->isEnd;
}
if(word[i]=='.'){
for(int j = 0;j<26;j++){
if(now->node[j]!=NULL){
if(dfs(i+1,word,now->node[j]))
return true;
}
}
return false;
}
int index = word[i]-'a';
if(now->node[index] == NULL)
return false;
else
return dfs(i+1,word,now->node[index]);
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/