Design Search Autocomplete System using Trie
Create a prefix tree data structure with three main operations: addWord
, findWord
, and hasPrefix
.
The addWord
method inserts a word into the prefix tree. The findWord
method returns whether a given word exists in the tree. The hasPrefix
method checks if there is any word in the structure that starts with the given prefix.
PrefixTree tree = new PrefixTree(); tree.addWord("banana"); tree.findWord("banana"); // returns true tree.findWord("ban"); // returns false tree.hasPrefix("ban"); // returns true tree.addWord("ban"); tree.findWord("ban"); // returns true