-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddandSearchWord.py
More file actions
49 lines (45 loc) · 1.52 KB
/
Copy pathAddandSearchWord.py
File metadata and controls
49 lines (45 loc) · 1.52 KB
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
class TrieNode:
def __init__(self):
self.children = [None]*26
self.isend = False
def put(self, char, node) -> None:
index = ord(char)-ord('a')
self.children[index] = node
def get(self, char):
index = ord(char)-ord('a')
return self.children[index]
def containskey(self, char) -> bool:
index = ord(char)-ord('a')
return self.children[index] is not None
class WordDictionary:
def __init__(self):
self.node = TrieNode()
def addWord(self, word: str) -> None:
node = self.node
for char in word:
if not node.containskey(char):
node.put(char, TrieNode())
node = node.get(char)
node.isend = True
def searchtree(self,node, word: str) -> bool:
for i in range(len(word)):
char = word[i]
if char != '.':
if node.containskey(char):
node = node.get(char)
else:
return False
else:
for each in node.children:
if each != None:
if self.searchtree(each, word[i+1:len(word)]):
return True
return False
return node.isend
def search(self, word):
root = self.node
return self.searchtree(root, word)
word = "jacob"
obj = WordDictionary()
obj.addWord("jacob")
print(obj.search("j...e"))