-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignAddandSearchWordsDataStructure.py
47 lines (42 loc) · 1.34 KB
/
DesignAddandSearchWordsDataStructure.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
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.
"""
#if self.search(word):
# return
curr = self.trie
for i in word:
if i not in curr:
curr[i] = {}
curr = curr[i]
curr['*'] = ''
return
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.
"""
def dfs(word, curr_trie):
if word == '' and '*' in curr_trie:
return True
if word == '' and '*' not in curr_trie:
return False
if word[0] != '.':
if word[0] not in curr_trie:
return False
return dfs(word[1:], curr_trie[word[0]])
else:
for k in curr_trie:
if dfs(word[1:], curr_trie[k]):
return True
return False
return dfs(word, self.trie)
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)