-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm208.py
43 lines (33 loc) · 938 Bytes
/
m208.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
class Trie:
def __init__(self):
self.trie = {}
def insert(self, word: str) -> None:
curr = self.trie
for c in word :
if c in curr :
curr = curr[c]
else :
curr[c] = {}
curr = curr[c]
curr['#'] = True
def search(self, word: str) -> bool:
curr = self.trie
for c in word :
if c not in curr :
return False
curr = curr[c]
if '#' in curr :
return True
return False
def startsWith(self, prefix: str) -> bool:
curr = self.trie
for c in prefix :
if c not in curr :
return False
curr = curr[c]
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)