-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path211DesignAddandSearchWordsDataStructure.py
More file actions
53 lines (45 loc) · 1.25 KB
/
Copy path211DesignAddandSearchWordsDataStructure.py
File metadata and controls
53 lines (45 loc) · 1.25 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
50
51
52
53
class WordDictionary(object):
def __init__(self):
self.database = dict()
self.flag = '-'
def addWord(self, word):
"""
:type word: str
:rtype: None
"""
note = self.database
for i in word:
if i not in note:
note[i] = {}
note = note[i]
note[self.flag] = True
def search(self, word):
"""
:type word: str
:rtype: bool
"""
note = self.database
notes = []
notes.append(note)
for i in word:
tmp = []
if i == '.':
for j in notes:
for k in j:
if k != self.flag:
tmp.append(j[k])
else:
for j in notes:
if i in j:
tmp.append(j[i])
if not tmp:
return False
notes = tmp
for i in notes:
if self.flag in i:
return True
return False
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)