-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add_and_Search_Word_Data_structure_design.cpp
70 lines (64 loc) · 2 KB
/
Add_and_Search_Word_Data_structure_design.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
62
63
64
65
66
67
68
69
70
//https://leetcode.com/problems/add-and-search-word-data-structure-design/description/
struct TrieNode {
TrieNode* chars[26];
bool isEnd;
};
class WordDictionary {
public:
/** Initialize your data structure here. */
TrieNode* root;
WordDictionary() {
root = getNode();
}
TrieNode* getNode () {
TrieNode* root = new TrieNode;
root -> isEnd = false;
for(int i = 0; i < 26; i++) {
root -> chars[i] = NULL;
}
return root;
}
/** Adds a word into the data structure. */
void addWord(string word) {
TrieNode* temp = root;
for(int i = 0; i < word.size(); i++) {
int index = word[i] - 'a';
if(temp -> chars[index] == NULL)
temp -> chars[index] = getNode();
temp = temp -> chars[index];
}
temp -> 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 searchHelper(word, root, 0);
}
bool searchHelper(string word, TrieNode* temp, int pos) {
if(pos == word.size()) {
if(temp -> isEnd)
return true;
return false;
}
bool found = false;
if(word[pos] == '.') {
for(int i = 0; i < 26; i++) {
if(temp -> chars[i] != NULL) {
found = searchHelper (word, temp -> chars[i], pos + 1);
if(found)
break;
}
}
} else {
int index = word[pos] - 'a';
if (temp -> chars[index] != NULL)
found = searchHelper (word, temp -> chars[index], pos + 1);
}
return found;
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/