A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
Trie() Initializes the trie object.insert(word) Inserts the string word into the trie.search(word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.startsWith(prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]][null, null, true, false, true, null, true]1 <= word.length, prefix.length <= 2000word and prefix consist only of lowercase English lettersAt most 3 * 10^4 calls in total will be made to insert, search, and startsWithExpected time complexity: O(m) per operationRun your code to see results
Use Cmd+Enter to run