-
Notifications
You must be signed in to change notification settings - Fork 77
/
trie.py
51 lines (41 loc) · 1.24 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
# coding: utf-8
"""
Trie
https://en.wikipedia.org/wiki/Trie
"""
class Trie:
def __init__(self):
self.root = {}
self.size = 0
def __len__(self):
return self.size
def insert(self, word: str) -> None:
if not word:
raise ValueError('word cannot be empty')
node = self.root
for char in word:
node = node.setdefault(char, {})
# Since the searched word might be shorter than the word in Trie,
# search() needs a way to determine whether the searching matches the exact word,
# For instance, we have `hammer`in Trie and we are searching for `hammers` which should return False.
node['$word_end'] = True
self.size += 1
def search(self, word: str) -> bool:
node = self.root
for char in word:
try:
node = node[char]
except KeyError:
return False
if '$word_end' in node:
return True
else:
return False
def startsWith(self, prefix: str) -> bool:
node = self.root
for char in prefix:
try:
node = node[char]
except KeyError:
return False
return True