-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordDictionary_2.py
More file actions
53 lines (45 loc) · 1.28 KB
/
WordDictionary_2.py
File metadata and controls
53 lines (45 loc) · 1.28 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):
"""
Initialize your data structure here.
"""
self.di = {}
def addWord(self, word):
"""
Adds a word into the data structure.
:type word: str
:rtype: void
"""
l = len(word)
if l not in self.di:
self.di[l] = [word]
else:
self.di[l].append(word)
def search(self, word):
"""
Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
:type word: str
:rtype: bool
"""
l = len(word)
if l not in self.di:
return False
if '.' not in word:
return word in self.di[l]
for words in self.di[l]:
for j in range(l):
if word[j] != '.' and word[j] != words[j]:
break
elif j == l - 1:
return True
return False
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
if __name__ == '__main__':
t = WordDictionary()
t.addWord("add")
t.addWord("tdd")
t.addWord("serc")
t.search("..e")