-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path208_medium_[trie]_implement_trie.py
61 lines (41 loc) · 1.43 KB
/
208_medium_[trie]_implement_trie.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
class Node:
def __init__(self, val, is_word=False):
self.val = val
self.children = {}
self.is_word = is_word
class Trie:
def __init__(self):
self.children = {}
def insert(self, word: str) -> None:
last_node = self
for c in word:
if not last_node.children.get(c):
last_node.children[c] = Node(c)
last_node = last_node.children[c]
last_node.is_word = True
def search(self, word: str) -> bool:
last_node = self._search_prefix(word)
return last_node is not None and last_node.is_word
def startsWith(self, prefix: str) -> bool:
last_node = self._search_prefix(prefix)
return last_node is not None
def _search_prefix(self, prefix: str) -> bool:
last_node = self
for c in prefix:
if not last_node.children.get(c):
return None
last_node = last_node.children[c]
return last_node
if __name__ == '__main__':
# Your Trie object will be instantiated and called as such:
trie = Trie();
trie.insert("apple")
assert trie.search("apple") == True
assert trie.search("app") == False
assert trie.startsWith("app") == True
trie.insert("app")
assert trie.search("app") == True
trie = Trie()
trie.insert("a")
assert trie.search("a") == True
assert trie.startsWith("a") == True