-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordDictionary.cpp
61 lines (54 loc) · 1.6 KB
/
wordDictionary.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
59
60
61
class TrieNode {
public:
bool is_leaf;
TrieNode* children[26];
TrieNode() {
is_leaf = false;
memset(children, NULL, sizeof(children));
}
};
class WordDictionary {
private:
TrieNode* root = new TrieNode();
bool search(const char* word, TrieNode* node) {
for(int i = 0; word[i] && node; ++i) {
if(word[i] != '.') {
node = node->children[word[i] - 'a'];
} else {
TrieNode* temp = node;
for(int j = 0; j < 26; ++j) {
node = temp->children[j];
if(search(word + i + 1, node)) {
return true;
}
}
}
}
return node && node->is_leaf;
}
public:
/** Initialize your data structure here. */
WordDictionary() {
}
/** Adds a word into the data structure. */
void addWord(string word) {
TrieNode* node = root;
for(char c: word) {
if(!node->children[c - 'a']) {
node->children[c - 'a'] = new TrieNode();
}
node = node->children[c - 'a'];
}
node->is_leaf = 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 search(word.c_str(), root);
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/