-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathok_implement_trie.py
53 lines (43 loc) · 1.3 KB
/
ok_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
"""208. Implement Trie (Prefix Tree)
Medium
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
All inputs are guaranteed to be non-empty strings.
Details
Runtime: 120 ms, faster than 98.95% of Python3 online submissions for Implement Trie (Prefix Tree).
Memory Usage: 25.9 MB, less than 66.67% of Python3 online submissions for Implement Trie (Prefix Tree).
"""
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = {}
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
node = self.root
for ch in word:
if ch not in node:
node[ch] = {}
node = node[ch]
node['#'] = None
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
node = self.root
for ch in word:
if ch not in node:
return False
node = node[ch]
return '#' in node
def startsWith(self, word: str) -> bool:
node = self.root
for ch in word:
if ch not in node:
return False
node = node[ch]
return True