-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path211.py
40 lines (33 loc) · 1.13 KB
/
211.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
class Node:
def __init__(self, c = None):
self.c = c
self.end = False
self.d = defaultdict(Node)
class WordDictionary:
def __init__(self):
self.root = Node()
def addWord(self, word: str) -> None:
curr = self.root
for char in word:
if char not in curr.d:
curr.d[char] = Node(char)
curr = curr.d[char]
curr.end = True
def search(self, word: str) -> bool:
def recursion(node: Node, i: int, word: str):
if i == len(word):
return node.end
if word[i] == '.':
for next in node.d:
if recursion(node.d[next], i+1, word):
return True
return False
elif word[i] in node.d:
if recursion(node.d[word[i]], i+1, word):
return True
return False
return recursion(self.root, 0, word)
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)