-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm211.py
43 lines (31 loc) · 1.1 KB
/
m211.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
class WordDictionary:
def __init__(self):
self.trie = {}
def addWord(self, word: str) -> None:
curr = self.trie
for c in word :
if c not in curr :
curr[c] = {}
curr = curr[c]
curr[False] = True # Mark end of a word
def search(self, word: str) -> bool:
curr = self.trie
for i, c in enumerate(word) :
if c == '.' :
return any(self.searchFrom(word[i + 1:], curr[x]) for x in curr if x)
if c not in curr :
return False
curr = curr[c]
return False in curr
def searchFrom(self, word, curr: dict) -> bool :
for i, c in enumerate(word) :
if c == '.' :
return any(self.searchFrom(word[i + 1:], curr[x]) for x in curr if x)
if c not in curr :
return False
curr = curr[c]
return False in curr
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)