-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.cpp
More file actions
77 lines (63 loc) · 1.9 KB
/
Dictionary.cpp
File metadata and controls
77 lines (63 loc) · 1.9 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <vector>
#include <string>
struct TrieNode {
std::vector<TrieNode*> children;
bool endOfWord;
TrieNode() : children(26, nullptr), endOfWord(false) {}
};
class WordDictionary {
private:
TrieNode* root;
public:
WordDictionary() {
root = new TrieNode();
}
void addWord(std::string word) {
TrieNode* curr = root;
for (int i=0; i<word.size(); i++) {
// create child node if it doesn't exist
if (curr->children[word[i]-'a'] == nullptr) {
TrieNode* temp = new TrieNode();
curr->children[word[i]-'a'] = temp;
}
curr = curr->children[word[i]-'a'];
if (i==word.size()-1) {
// Assign end of word
curr->endOfWord = true;
}
}
}
bool search(std::string word) {
TrieNode* curr = root;
return searchNode(root, 0, word);
}
// root is a parent trie node and index marks the character to search for in the
// node's children
bool searchNode(TrieNode* root, int index, std::string& word) {
// Check if the child exists
if (root == nullptr) {
return false;
}
// If we've reached the end of the word
if (index == word.size()) {
return root->endOfWord;
}
// Process the next letter
if (word[index] != '.') {
return searchNode(root->children[word[index]-'a'], index+1, word);
} else {
for (int i=0; i<26; i++) {
if (searchNode(root->children[i], index+1, word)) {
return true;
}
}
return false;
}
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/