generated from foambubble/foam-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsolution1.py
More file actions
38 lines (31 loc) · 1.1 KB
/
solution1.py
File metadata and controls
38 lines (31 loc) · 1.1 KB
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
class Node:
def __init__(self,c,end=False):
self.c = c
self.child = {}
self.end = end
class Trie:
def __init__(self):
self.root = Node("/")
def insert(self, word: str) -> None:
cur = self.root
for c in word:
if c not in cur.child: cur.child[c] = Node(c)
cur = cur.child[c]
cur.end = True
def search(self, word: str) -> bool:
cur = self.root
for c in word:
if c not in cur.child: return False
cur = cur.child[c]
return cur.end
def startsWith(self, prefix: str) -> bool:
cur = self.root
for c in prefix:
if c not in cur.child: return False
cur = cur.child[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)