-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0211.cpp
More file actions
31 lines (28 loc) · 976 Bytes
/
Copy path0211.cpp
File metadata and controls
31 lines (28 loc) · 976 Bytes
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
class WordDictionary {
vector<WordDictionary *> children;
bool isEndOfWord = false;
;
public:
WordDictionary() : children(vector<WordDictionary *>(26, nullptr)) {}
void addWord(string word) {
WordDictionary *crnt = this;
for (char c : word) {
if (crnt->children[c - 'a'] == nullptr) crnt->children[c - 'a'] = new WordDictionary();
crnt = crnt->children[c - 'a'];
}
crnt->isEndOfWord = true;
}
bool search(string word) {
WordDictionary *crnt = this;
for (int i = 0; i < word.length(); ++i) {
if (word[i] == '.') {
for (auto c : crnt->children)
if (c && c->search(word.substr(i + 1))) return true;
return false;
}
if (crnt->children[word[i] - 'a'] == nullptr) return false;
crnt = crnt->children[word[i] - 'a'];
}
return crnt && crnt->isEndOfWord;
}
};