-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1032.cpp
More file actions
77 lines (73 loc) · 1.79 KB
/
1032.cpp
File metadata and controls
77 lines (73 loc) · 1.79 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class StreamChecker
{
struct trieNode
{
trieNode *children[26];
bool completeword =false;
};
typedef struct trieNode tnode;
int maxwsz;
tnode *root;
list<char> qst;
public:
tnode* getNode()
{
tnode* nnd = new tnode();
for(int i=0; i<25; i++) {
nnd->children[i]=0;
}
return nnd;
}
void insert(string s, tnode* troot)
{
for(int i= s.size()-1; i>=0; i--)
{
if(!troot->children[s[i]-'a']) {
troot->children[s[i]-'a'] = getNode();
}
troot = troot->children[s[i]-'a'];
}
troot->completeword = true;
}
StreamChecker(vector<string>& words) {
//create the db
maxwsz = 0;
root = getNode();
for(int i=0; i<words.size(); ++i)
{
insert(words[i],root);
if(maxwsz<words[i].size())
maxwsz= words[i].size();
}
//cout<<"done constructing"<<endl;
}
bool searchString(tnode* troot)
{
list<char>::iterator it = qst.end();
it--;
for(int i = 0; i<qst.size();--it,++i)
{
if(troot->children[*it-'a']==0)
return false;
troot = troot->children[*it-'a'];
if(troot->completeword)
return true;
}
return troot->completeword;
}
bool query(char letter)
{
if(qst.size() >=maxwsz ) {
qst.erase(qst.begin());
}
qst.push_back(letter);
if(searchString(root))
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);
*/