-
Notifications
You must be signed in to change notification settings - Fork 0
/
1032_streamChecker.py
37 lines (29 loc) · 1.07 KB
/
1032_streamChecker.py
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
# 1032. Stream of Characters
# Anish Malla's Solution: https://www.youtube.com/watch?v=Z9J2SEORBug
# https://leetcode.com/problems/stream-of-characters/discuss/807900/Python-Simple-Trie-Solution-Explained-(code-%2B-video
class StreamChecker:
def __init__(self, words: List[str]):
self.trie = {}
self.nodes = []
for word in words:
node = self.trie
for char in word:
if char not in node:
node[char] = {}
node = node[char]
node['end'] = True
def query(self, letter: str) -> bool:
self.nodes.append(self.trie)
temp = False
new_nodes = []
for node in self.nodes:
if letter in node:
node = node[letter]
if 'end' in node:
temp = True
new_nodes.append(node)
self.nodes = new_nodes
return temp
# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)