classTrieNode(object):def__init__(self):self.isWord=Falseself.children=dict()classWordDictionary(object):def__init__(self):""" initialize your data structure here. """self.root=TrieNode()defaddWord(self,word):""" Adds a word into the data structure. :type word: str :rtype: void """node=self.rootforletterinword:child=node.children.get(letter)ifchild==None:child=TrieNode()node.children[letter]=childnode=childnode.isWord=Truedefsearch(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 """returnself.find(self.root,word)deffind(self,node,word):ifword=="":returnnode.isWordletter=word[0]ifletter=='.':forchildinnode.children:ifself.find(node.children[child],word[1:]):returnTruechild=node.children.get(letter)ifchild==None:returnFalseelse:returnself.find(child,word[1:])returnFalse# Your WordDictionary object will be instantiated and called as such:# wordDictionary = WordDictionary()# wordDictionary.addWord("word")# wordDictionary.search("pattern")