-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamofCharacters.cpp
More file actions
58 lines (50 loc) · 1.33 KB
/
streamofCharacters.cpp
File metadata and controls
58 lines (50 loc) · 1.33 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
52
53
54
55
56
57
58
// Source: https://leetcode.com/problems/stream-of-characters/
// Author: Miao Zhang
// Date: 2021-04-05
class TrieNode {
public:
~TrieNode() {
for (auto node: next_) {
delete node;
}
}
void reverseBuild(const string&s, int i) {
if (i == -1) {
is_word_ = true;
return;
}
const int idx = s[i] - 'a';
if (!next_[idx]) next_[idx] = new TrieNode();
next_[idx]->reverseBuild(s, i - 1);
}
bool reverseSearch(const string& s, int i) {
if (i == -1 || is_word_) return is_word_;
int idx = s[i] - 'a';
if (!next_[idx]) return false;
return next_[idx]->reverseSearch(s, i - 1);
}
private:
bool is_word_ = false;
TrieNode* next_[26] = {0};
};
class StreamChecker {
public:
StreamChecker(vector<string>& words) {
root_ = std::make_unique<TrieNode>();
for (const string& w: words) {
root_->reverseBuild(w, w.length() - 1);
}
}
bool query(char letter) {
s_ += letter;
return root_->reverseSearch(s_, s_.length() - 1);
}
private:
string s_;
unique_ptr<TrieNode> root_;
};
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker* obj = new StreamChecker(words);
* bool param_1 = obj->query(letter);
*/