classWordDictionary:def__init__(self):"""
Initialize your data structure here.
"""
self.trie ={}defaddWord(self, word:str)->None:"""
Adds a word into the data structure.
"""
node = self.trie
for c in word:
node = node.setdefault(c,{})
node['end']=1defsearch(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.
"""return self.helper(word, self.trie)defhelper(self, word, root):ifnot word:return root.get('end',0)==1ifnot root:returnFalse
node = root
for c in word:if c !='.':if c notin node:returnFalsereturn self.helper(word[1:], node[c])else:for k, v in node.items():if k !='end'and self.helper(word[1:], node[k]):returnTruereturnFalse