-
Notifications
You must be signed in to change notification settings - Fork 0
/
211-design-add-and-search-words-data-structure.py
53 lines (35 loc) · 1.28 KB
/
211-design-add-and-search-words-data-structure.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
class Node:
def __init__(self, val=False):
self.refs = [None] * 26
self.val = val
class WordDictionary:
def __init__(self):
self.root = Node()
def addWord(self, word: str) -> None:
current = self.root
for letter in word:
index = ord(letter) - ord('a')
if not current.refs[index]:
current.refs[index] = Node()
current = current.refs[index]
current.val = True
def search(self, word: str) -> bool:
return self.search_rec(word, self.root)
def search_rec(self, word: str, node: Node):
if len(word) == 0:
return node.val
if word[0] == '.':
for i in range(26):
if node.refs[i] and self.search_rec(word[1:], node.refs[i]):
return True
return False
else:
index = ord(word[0]) - ord('a')
if node.refs[index]:
return self.search_rec(word[1:], node.refs[index])
else:
return False
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)