-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path208_implement-trie-prefix-tree.py
53 lines (35 loc) · 1.22 KB
/
208_implement-trie-prefix-tree.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
# https://leetcode.com/problems/implement-trie-prefix-tree/
class Node:
def __init__(self, value=None):
self.value = value
self.children = {}
self.is_last = False
class Trie:
def __init__(self):
self.head = Node()
def insert(self, word: str) -> None:
cur_node = self.head
for c in word:
if c not in cur_node.children:
cur_node.children[c] = Node(c)
cur_node = cur_node.children[c]
cur_node.is_last = True
def _find_node(self, word: str) -> Node:
cur_node = self.head
for c in word:
if c in cur_node.children:
cur_node = cur_node.children[c]
else:
return None
return cur_node
def search(self, word: str) -> bool:
found = self._find_node(word)
return found.is_last if found else False
def startsWith(self, prefix: str) -> bool:
found = self._find_node(prefix)
return True if found else False
# 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)