-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathok_add_and_search_word.py
99 lines (80 loc) · 2.68 KB
/
ok_add_and_search_word.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
211. Add and Search Word - Data structure design
Medium
Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or `.`.
A `.` means it can represent any one letter.
Note:
You may assume that all words are consist of lowercase letters a-z.
Details
Runtime: 276 ms, faster than 82.23% of Python3 online submissions for Add and Search Word - Data structure design.
Memory Usage: 23.2 MB, less than 91.30% of Python3 online submissions for Add and Search Word - Data structure design.
"""
class WordDictionary:
def __init__(self):
"""
Initialize your data structure here.
"""
self.trie = {}
def addWord(self, word: str) -> None:
"""
Adds a word into the data structure.
"""
node = self.trie
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 data structure. A word could contain the dot character '.' to represent any one letter.
"""
node = self.trie
return self.__search_dfs(word, 0, node) is True
def __search_bfs(self, word: str, trie) -> bool:
""" 332 - 360 ms """
q = [(trie, 0)]
while q:
node, i = q.pop(0)
if i == len(word):
if '#' in node:
return True
else:
continue
if word[i] == '.':
for k in node.keys():
if k != '#':
q.append((node[k], i + 1))
elif word[i] in node:
q.append((node[word[i]], i + 1))
return False
def __search_dfs(self, word, w_idx, trie):
""" 272 ms """
if w_idx == len(word):
return '#' in trie
if word[w_idx] == '.':
for k in trie:
if k != '#' and self.__search_dfs(word, w_idx + 1, trie[k]):
return True
elif word[w_idx] in trie and self.__search_dfs(word, w_idx + 1, trie[word[w_idx]]):
return True
def _main():
wd = WordDictionary()
wd.addWord('bad')
wd.addWord('dad')
wd.addWord('mad')
assert not wd.search('pad')
assert wd.search('bad')
assert wd.search('.ad')
assert wd.search('b.d')
assert wd.search('b..')
assert not wd.search('b...')
assert wd.search('...')
assert not wd.search('.at')
wd.addWord('bat')
assert wd.search('.at')
if __name__ == '__main__':
_main()