-
Notifications
You must be signed in to change notification settings - Fork 8
/
solution.py
153 lines (132 loc) · 3.52 KB
/
solution.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
15 / 15 test cases passed.
Runtime: 236 ms
Memory Usage: 33.6 MB
"""
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.next = [None] * 26
self.is_end = False
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
node = self
for ch in word:
index = ord(ch) - ord('a')
if not node.next[index]:
node.next[index] = Trie()
node = node.next[index]
node.is_end = True
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
node = self
for ch in word:
index = ord(ch) - ord('a')
if not node.next[index]:
return False
node = node.next[index]
return node.is_end
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
node = self
for ch in prefix:
index = ord(ch) - ord('a')
if not node.next[index]:
return False
node = node.next[index]
return True
"""
15 / 15 test cases passed.
Runtime: 180 ms
Memory Usage: 32.5 MB
"""
class Trie2:
def __init__(self):
"""
Initialize your data structure here.
"""
self.lookup = { '#': False }
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
node = self
for ch in word:
if ch not in node.lookup:
node.lookup[ch] = Trie()
node = node.lookup[ch]
node.lookup['#'] = True
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
node = self
for ch in word:
if ch not in node.lookup:
return False
node = node.lookup[ch]
return node.lookup['#']
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
node = self
for ch in prefix:
if ch not in node.lookup:
return False
node = node.lookup[ch]
return True
"""
15 / 15 test cases passed.
Runtime: 136 ms
Memory Usage: 27.7 MB
"""
class Trie3:
def __init__(self):
"""
Initialize your data structure here.
"""
self.lookup = {}
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
trie = self.lookup
for ch in word:
if ch not in trie:
trie[ch] = {}
trie = trie[ch]
trie['#'] = '#'
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
trie = self.lookup
for ch in word:
if ch not in trie:
return False
trie = trie[ch]
return '#' in trie
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
trie = self.lookup
for ch in prefix:
if ch not in trie:
return False
trie = trie[ch]
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)