211. 添加与搜索单词 - 数据结构设计
为保证权益,题目请参考 211. 添加与搜索单词 - 数据结构设计(From LeetCode).
解决方案1
Python
python
# 211. 添加与搜索单词 - 数据结构设计
# https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/
################################################################################
class WordTreeNode:
def __init__(self, letter: str) -> None:
self.letter = letter
self.isEnd = False
self.children = {}
def setIsEnd(self, isEnd: bool) -> None:
self.isEnd = isEnd
def addChild(self, child: str) -> None:
if child not in self.children:
self.children[child] = WordTreeNode(child)
def getChild(self, child: str):
if child in self.children:
return self.children[child]
return None
def getChildren(self):
return self.children.values()
class WordDictionary:
def __init__(self):
self.root = WordTreeNode("#")
def addWord(self, word: str) -> None:
t = self.root
for letter in word:
t.addChild(letter)
t = t.getChild(letter)
t.setIsEnd(True)
def search(self, word: str) -> bool:
def dfs(tNode: WordTreeNode, word: str, index: int):
if index == len(word):
return tNode.isEnd
tmp = word[index]
if tmp == ".":
for t in tNode.getChildren():
if dfs(t, word, index + 1):
return True
return False
else:
t = tNode.getChild(tmp)
if t is not None:
return dfs(t, word, index + 1)
else:
return False
t = self.root
return dfs(t, word, 0)
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
################################################################################
if __name__ == "__main__":
pass
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
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