-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay-23 Stream of Characters
51 lines (44 loc) · 1.15 KB
/
Day-23 Stream of Characters
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 {
class Trie{
Trie[] next=new Trie[26];
boolean end;
}
Trie root;
List<Integer> query;
public StreamChecker(String[] words) {
root=new Trie();
query=new ArrayList<>();
for(String s:words){
char[] c=s.toCharArray();
Trie t=root;
for(int i=c.length-1;i>=0;i--){
if(t.next[c[i]-'a']==null){
t.next[c[i]-'a']=new Trie();
}
t=t.next[c[i]-'a'];
}
t.end=true;
}
}
public boolean query(char letter) {
query.add(letter-'a');
Trie t=root;
int i=query.size()-1;
while(i>=0){
t=t.next[query.get(i)];
if(t!=null && t.end){
return true;
}
else if(t==null){
return false;
}
i--;
}
return false;
}
}
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker obj = new StreamChecker(words);
* boolean param_1 = obj.query(letter);
*/