-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement-trie-prefix-tree.py3
More file actions
53 lines (39 loc) · 1.39 KB
/
implement-trie-prefix-tree.py3
File metadata and controls
53 lines (39 loc) · 1.39 KB
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) (2/22/57464)
# Runtime: 50 ms (42.49%) Memory: 32.22 MB (25.92%)
# Time Complexity O(n) where n is the length of the word to search or insert
# Space Complexity O(n) where n is the length of the word
class Trie:
class TrieNode:
def __init__(self):
self.children = {} # hash map
self.endWord = False
def __init__(self):
self.root = self.TrieNode()
def insert(self, word: str) -> None:
currNode = self.root
for c in word:
# if not present, add it
if c not in currNode.children:
currNode.children[c] = self.TrieNode()
# otherwise retrive it
currNode = currNode.children[c]
currNode.endWord = True
def search(self, word: str) -> bool:
currNode = self.root
for c in word:
if c not in currNode.children:
return False
currNode = currNode.children[c]
return currNode.endWord
def startsWith(self, prefix: str) -> bool:
currNode = self.root
for c in prefix:
if c not in currNode.children:
return False
currNode = currNode.children[c]
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)