-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1032.cpp
More file actions
51 lines (45 loc) · 1.2 KB
/
1032.cpp
File metadata and controls
51 lines (45 loc) · 1.2 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
class StreamChecker {
public:
struct TrieNode{
vector<TrieNode*> tree;
bool is_word;
TrieNode(){
tree = vector<TrieNode*>(26,NULL);
is_word = false;
}
};
TrieNode* root;
string word;
void insert(vector<string>& words){
for(string s: words){
reverse(s.begin(),s.end());
TrieNode* tmp = root;
for(auto ch: s){
if(!tmp->tree[ch-'a'])
tmp->tree[ch-'a'] = new TrieNode();
tmp = tmp->tree[ch-'a'];
}
tmp->is_word = true;
}
}
StreamChecker(vector<string>& words) {
root = new TrieNode();
word = "";
insert(words);
}
bool query(char letter) {
word += letter;
TrieNode* tmp = root;
for(int i=word.size()-1; i>=0 && tmp; i--){
tmp = tmp->tree[word[i]-'a'];
if(tmp && tmp->is_word)
return true;
}
return false;
}
};
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker* obj = new StreamChecker(words);
* bool param_1 = obj->query(letter);
*/