-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
42 lines (35 loc) · 1.13 KB
/
solution.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
class TrieNode:
def __init__(self):
self.links = {}
self.end = False
class WordDictionary:
def __init__(self):
self.max_length = 0
self.root = TrieNode()
def addWord(self, word: str):
node = self.root
l = 0
for w in word:
if w not in node.links:
node.links[w] = TrieNode()
node = node.links[w]
l += 1
self.max_length = max(self.max_length, l)
node.end = True
def search(self, word: str) -> bool:
if len(word) > self.max_length:
return False
def helper(index: int, node: TrieNode):
for index in range(index, len(word)):
c = word[index]
if c == ".":
for child in node.links.values():
if helper(index+1, child):
return True
return False
else:
if c not in node.links:
return False
node = node.links[c]
return node.end
return helper(0, self.root)