Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TrieNode:
- def __init__(self):
- self.children={}
- self.isword=False
- class WordDictionary:
- def __init__(self):
- """
- Initialize your data structure here.
- """
- self.root=TrieNode()
- def addWord(self, word: str) -> None:
- """
- Adds a word into the data structure.
- """
- if not word:
- return
- node=self.root
- for c in word:
- if c not in node.children:
- node.children[c]=TrieNode()
- node=node.children[c]
- node.isword=True
- def search_helper(self,node,word)->bool:
- if not node or not word:
- return False
- for i in len(word[i]):
- if word[i]=='.':
- for child in node.children.values():
- if self.search_helper(child,word[i+1:]):
- return True
- else:
- if c not in node.children:
- return False
- node=node.children[word[i]]
- return node.isword
- def search(self, node, word) -> bool:
- """
- Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
- """
- return self.search_helper(self.root,word)
- # Your WordDictionary object will be instantiated and called as such:
- # obj = WordDictionary()
- # obj.addWord(word)
- # param_2 = obj.search(word)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement