Implement a trie (prefix tree) with insert, search, and startsWith methods. The trie should support the following operations:
insert(word: string) => void: Inserts a word into the trie.search(word: string) => boolean: Returns true if the word is in the trie, otherwise false.startsWith(prefix: string) => boolean: Returns true if there is a word in the trie that starts with the given prefix, otherwise false.YouYou may assume that all input words consist of lowercase English letters.
t = Trie()
t.insert("apple")
t.search("apple")
t.search("app")
t.starts_with("app")
t.insert("app")
t.search("app")[True, False, True, True]
After inserting 'apple', searching for 'apple' returns true, while searching for 'app' returns false. However, 'app' is a prefix of 'apple', so startsWith('app') returns true. After inserting 'app', searching for 'app' now returns true.
t = Trie()
t.insert("banana")
t.starts_with("ban")
t.search("ban")
t.insert("band")
t.starts_with("ban")
t.search("band")[True, False, True, True]
After inserting banana into the trie, startsWith('ban') returns true since 'ban' is a prefix of 'banana'. However, searching for 'ban' returns false as it is not a complete word in the trie. After inserting 'band', startsWith('ban') still returns true, and searching for 'band' returns true as it has been added to the trie.
t = Trie()
t.insert("apple")
t.search("apple")
t.search("app")
t.starts_with("app")
t.insert("app")
t.search("app")[True, False, True, True]