-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.py
58 lines (43 loc) · 1.41 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
class Trie:
def __init__(self):
self.trie = {}
self.end_of_word = "#"
def insert(self, word: str) -> None:
node = self.trie
for char in word:
node = node.setdefault(char, {})
node[self.end_of_word] = self.end_of_word
def search(self, word: str) -> bool:
node = self.trie
for char in word:
if char not in node:
return False
node = node[char]
return self.end_of_word in node
def startsWith(self, prefix: str) -> bool:
node = self.trie
for char in prefix:
if char not in node:
return False
node = node[char]
return True
# Time complexity: O(n), where n is the length of the word
# Space complexity: O(n)
# 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)
import unittest
class TestTrie(unittest.TestCase):
def setUp(self):
self.trie = Trie()
def test_trie(self):
self.trie.insert("apple")
self.assertTrue(self.trie.search("apple"))
self.assertFalse(self.trie.search("app"))
self.assertTrue(self.trie.startsWith("app"))
self.assertFalse(self.trie.search("orange"))
self.assertFalse(self.trie.startsWith("orange"))
if __name__ == '__main__':
unittest.main()