208. Implement Trie Prefix Tree
208. Implement Trie (Prefix Tree)
class Trie:
def __init__(self):
self.root = {}
def insert(self, word: str) -> None:
node = self.root
for c in word:
if c not in node:
node[c] = {}
node = node[c]
node["#"] = True
def search(self, word: str) -> bool:
node = self.root
for c in word:
if c not in node:
return False
node = node[c]
return "#" in node
def startsWith(self, prefix: str) -> bool:
node = self.root
for c in prefix:
if c not in node:
return False
node = node[c]
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
class TrieNode:
# Initialize your data structure here.
def __init__(self):
self.children = defaultdict(TrieNode)
self.is_word = False
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = TrieNode()
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
t = self.root
for char in word:
t = t.children[char]
t.is_word = True
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
t = self.root
for char in word:
if char not in t.children:
return False
t = t.children[char]
return t.is_word
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
t = self.root
for char in prefix:
if char not in t.children:
return False
t = t.children[char]
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)