-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream-of-characters.py
More file actions
32 lines (27 loc) · 897 Bytes
/
Copy pathstream-of-characters.py
File metadata and controls
32 lines (27 loc) · 897 Bytes
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
from typing import List
class StreamChecker:
def __init__(self, words: List[str]):
self.rev_trie = self.create_rev_trie(words)
self.stream = []
def query(self, letter: str) -> bool:
self.stream.append(letter)
return self.find()
def create_rev_trie(self, words):
root = {}
for word in words:
node = root
for i in range(len(word)-1, -1, -1):
if word[i] not in node:
node[word[i]] = {}
node = node[word[i]]
node['#'] = True
return root
def find(self):
node = self.rev_trie
for i in range(len(self.stream)-1, -1, -1):
if self.stream[i] in node:
node = node[self.stream[i]]
if '#' in node: return True
else:
return False
return False