-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstream_of_characters.cpp
61 lines (55 loc) · 1.55 KB
/
stream_of_characters.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
//standard trie class to insert character in the trie
class Trie {
public:
vector<Trie*> next;
bool isWord;
Trie () {
next = vector<Trie*>(26,nullptr);
isWord = false;
}
void insert (string& s) {
Trie* cur = this;
for (char& c: s) {
if (cur->next[c-'a']==nullptr) {
cur->next[c-'a'] = new Trie();
}
cur = cur->next[c-'a'];
}
cur->isWord = true;
}
};
class StreamChecker {
Trie root;
list<char> buffer;
int W_MAX;
public:
StreamChecker(vector<string>& words) {
W_MAX = 0;
for (auto& w: words) {
if (w.size() > W_MAX) W_MAX = w.size();
reverse(w.begin(), w.end());
root.insert(w);
}
}
bool query(char letter) {
buffer.push_back(letter);
if (buffer.size() == W_MAX+1) buffer.pop_front();
/*
* No point in writing input letters in an unbounded buffer.
* the maximum size of the words from input list is W_MAX. We don't need to store
* more characters than that.
*/
Trie* cur = &root;
for (auto it = buffer.rbegin(); it != buffer.rend(); ++it) {
if (cur->isWord) return true;
if (cur->next[*it -'a'] == nullptr) return false;
cur = cur->next[*it-'a'];
}
return (cur->isWord);
}
};
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker* obj = new StreamChecker(words);
* bool param_1 = obj->query(letter);
*/