You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classTrieNode:
def__init__(self):
self.isEndOfWord=Falseself.children= {}
classWordDictionary:
def__init__(self):
self.root=TrieNode()
defaddWord(self, word: str) ->None:
currentNode=self.rootforcinword:
ifcincurrentNode.children:
currentNode=currentNode.children[c]
else:
# Add a new node and update the pointercurrentNode.children[c] =TrieNode()
currentNode=currentNode.children[c]
# Flag the last node as the end of a wordcurrentNode.isEndOfWord=Truedefsearch(self, word: str) ->bool:
defdfs(k, node):
currentNode=node# Iterate over all characters left in the wordforiinrange(k, len(word)):
char=word[i]
ifchar=='.':
# Run a DFS over each of the node's child nodesforchildincurrentNode.children.values():
# If any of the children have a successful result we return Trueifdfs(i+1, child):
returnTruereturnFalseelse:
ifcharnotincurrentNode.children:
returnFalse# Move the pointer forwardcurrentNode=currentNode.children[char]
# Only return true if the final node is flagged as the end of a wordreturncurrentNode.isEndOfWord# Start at the first character of the word and the root notereturndfs(0, self.root)
# Your WordDictionary object will be instantiated and called as such:# obj = WordDictionary()# obj.addWord(word)# param_2 = obj.search(word)
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: