-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
34 lines (28 loc) · 849 Bytes
/
main.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
class Node:
def __init__(self):
self.children = {}
self.isWord = False
class Trie:
def __init__(self):
self.head = Node()
def insert(self, word: str) -> None:
point = self.head
for c in word:
if c not in point.children:
point.children[c] = Node()
point = point.children[c]
point.isWord = True
def search(self, word: str) -> bool:
point = self.head
for c in word:
if c not in point.children:
return False
point = point.children[c]
return point.isWord
def startsWith(self, prefix: str) -> bool:
point = self.head
for c in prefix:
if c not in point.children:
return False
point = point.children[c]
return True