class WordDictionary:
def __init__(self):
"""
Initialize your data structure here.
"""
self.myDict = {}
def addWord(self, word: str) -> None:
"""
Adds a word into the data structure.
"""
length = len(word)
if length in self.myDict:
self.myDict[length] += [word]
else:
self.myDict[length] = [word]
def search(self, word: str) -> bool:
"""
Returns if the word is in the data structure. A word could contain the dot character ‘.‘ to represent any one letter.
"""
length = len(word)
if length in self.myDict:
for auxiliary in self.myDict[length]:
index = 0
for index1 in range(length):
if word[index1] != auxiliary[index1] and word[index1] != ‘.‘:
break
elif word[index1] == auxiliary[index1] or word[index1] == ‘.‘:
index += 1
if index == length:
return True
if length not in self.myDict:
return False
return False
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)