-
Notifications
You must be signed in to change notification settings - Fork 1
/
1032-Stream-of-Characters.py
91 lines (83 loc) · 2.62 KB
/
1032-Stream-of-Characters.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Node:
def __init__(self):
self.children={}
self.endpoint=False
class StreamChecker:
# TLE
def __init__(self, words: List[str]):
root=Node()
for word in words:
node=root
for item in word:
if item not in node.children:
node.children[item]=Node()
node=node.children[item]
node.endpoint=True
self.root=root
self.set=set([])
def query(self, letter: str) -> bool:
current_set=set([])
flag=False
for node in self.set:
if letter in node.children:
tmp=node.children[letter]
if tmp.endpoint:
flag=True
current_set.add(tmp)
if letter in self.root.children:
current_set.add(self.root.children[letter])
if self.root.children[letter].endpoint:
flag=True
self.set=current_set
return flag
# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)
class Node:
def __init__(self):
self.children={}
self.endpoint=False
class StreamChecker:
# TLE
def __init__(self, words: List[str]):
root=Node()
for word in words:
node=root
for item in word:
if item not in node.children:
node.children[item]=Node()
node=node.children[item]
node.endpoint=True
self.root=root
self.lst=[]
def query(self, letter: str) -> bool:
self.lst=[item.children[letter] for item in self.lst+[self.root] if letter in item.children]
return any(item.endpoint for item in self.lst)
class Node:
def __init__(self):
self.children={}
self.endpoint=False
class StreamChecker:
def __init__(self, words: List[str]):
root=Node()
self.len=0
for word in words:
node=root
self.len=max(self.len,len(words))
for item in reversed(word):
if item not in node.children:
node.children[item]=Node()
node=node.children[item]
node.endpoint=True
self.root=root
self.buffer=""
def query(self, letter: str) -> bool:
self.buffer=(letter+self.buffer)[:self.len]
node=self.root
for item in self.buffer:
if item in node.children:
node=node.children[item]
if node.endpoint:
return True
else:
return False