class Trie {
public:
/** Initialize your data structure here. */
Trie() {
}
/** Inserts a word into the trie. */
void insert(string word) {
}
/** Returns if the word is in the trie. */
bool search(string word) {
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.arr = []
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
self.arr.append(word)
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
return word in self.arr
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
for word in self.arr:
if word.startswith(prefix):
return True
return False
# 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)