-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 14
38 lines (34 loc) · 845 Bytes
/
Day 14
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
class Trie {
public:
Trie() {}
void insert(string word) {
Trie* node = this;
for (char ch : word) {
ch -= 'a';
if (!node->next[ch]) { node->next[ch] = new Trie(); }
node = node->next[ch];
}
node->isword = true;
}
bool search(string word) {
Trie* node = this;
for (char ch : word) {
ch -= 'a';
if (!node->next[ch]) { return false; }
node = node->next[ch];
}
return node->isword;
}
bool startsWith(string prefix) {
Trie* node = this;
for (char ch : prefix) {
ch -= 'a';
if (!node->next[ch]) { return false; }
node = node->next[ch];
}
return true;
}
private:
Trie* next[26] = {};
bool isword = false;
};