-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path211.cpp
70 lines (67 loc) · 1.7 KB
/
211.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
class TrieNode {
public:
vector<TrieNode*> children;
bool isEnd;
TrieNode() {
this->children = vector<TrieNode*>(26, nullptr);
this->isEnd = false;
}
};
class Trie {
public:
TrieNode* root;
Trie() {
root = new TrieNode();
}
void insert(string& word) {
TrieNode* node = root;
for (auto& c : word) {
if (node->children[c - 'a'] == nullptr) {
node->children[c - 'a'] = new TrieNode();
}
node = node->children[c - 'a'];
}
node->isEnd = true;
}
bool search(string& word, int index) {
TrieNode* node = root;
return _search(root, word, index);
}
bool _search(TrieNode* node, string& word, int index) {
if (index == word.size()) {
return node->isEnd;
}
char c = word[index];
if (c == '.') {
for (int j = 0; j < 26; ++j) {
if (node->children[j] && _search(node->children[j], word, index + 1)) {
return true;
}
}
return false;
}
else {
if (node->children[c - 'a'] == nullptr) return false;
return _search(node->children[c - 'a'], word, index + 1);
}
}
};
class WordDictionary {
public:
Trie* trie;
WordDictionary() {
trie = new Trie();
}
void addWord(string word) {
trie->insert(word);
}
bool search(string word) {
return trie->search(word, 0);
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/