-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.py
114 lines (98 loc) · 2.99 KB
/
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# https://leetcode.com/problems/implement-trie-prefix-tree/
class Node:
def __init__(self, val):
self.val = val
self.children = {}
def add_children(self, val):
if val not in self.children:
self.children[val] = Node(val)
return self.children[val]
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = Node("/")
def insert(self, word):
"""
Inserts a word into the trie.
:type word: str
:rtype: void
"""
def insert_util(word, i, root):
if i == len(word):
root.add_children("$")
return
insert_util(word, i + 1, root.add_children(word[i]))
insert_util(word, 0, self.root)
def search(self, word):
"""
Returns if the word is in the trie.
:type word: str
:rtype: bool
"""
def search_util(word, i, root):
if i == len(word):
return "$" in root.children
return word[i] in root.children and search_util(word, i + 1, root.children[word[i]])
return search_util(word, 0, self.root)
def startsWith(self, prefix):
"""
Returns if there is any word in the trie that starts with the given prefix.
:type prefix: str
:rtype: bool
"""
def search_util(word, i, root):
if i == len(word):
return True
return word[i] in root.children and search_util(word, i + 1, root.children[word[i]])
return search_util(prefix, 0, self.root)
# 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)
class Trie_Iterative:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = Node("/")
def insert(self, word):
"""
Inserts a word into the trie.
:type word: str
:rtype: void
"""
root = self.root
for i in range(0, len(word)):
root = root.add_children(word[i])
root.add_children("$")
def search(self, word):
"""
Returns if the word is in the trie.
:type word: str
:rtype: bool
"""
root = self.root
ans = True
for i in range(0, len(word)):
ans = word[i] in root.children
if not ans:
break
root = root.children[word[i]]
return ans and "$" in root.children
def startsWith(self, prefix):
"""
Returns if there is any word in the trie that starts with the given prefix.
:type prefix: str
:rtype: bool
"""
root = self.root
ans = True
for i in range(0, len(prefix)):
ans = prefix[i] in root.children
if not ans:
break
root = root.children[prefix[i]]
return ans