Practice/TikTok/Leetcode 208. Implement Trie (Prefix Tree)
CodingMust
Design and implement a dictionary data structure that supports efficient word storage and retrieval operations. Your dictionary should handle three core operations:
The dictionary needs to efficiently handle strings containing only lowercase English letters (a-z).
Implement a Dictionary class with the following methods:
__init__() or constructor(): Initialize an empty dictionaryadd(word): Add a word to the dictionaryfind(word): Return true if the exact word exists in the dictionary, false otherwisehasPrefix(prefix): Return true if at least one word in the dictionary starts with the given prefix, false otherwiseEach method should execute efficiently relative to the length of the input string
Handle multiple operations on the same dictionary instance
word and prefix consist only of lowercase English lettersaddfind and hasPrefixExample 1:
` Operations: ["initialize", "add", "find", "find", "hasPrefix", "hasPrefix"] Arguments: [[], ["hello"], ["hello"], ["help"], ["hel"], ["hex"]] Output: [null, null, true, false, true, false] Explanation:
Example 2:
` Operations: ["initialize", "add", "add", "add", "hasPrefix", "find"] Arguments: [[], ["test"], ["testing"], ["tester"], ["test"], ["test"]] Output: [null, null, null, null, true, true] Explanation:
Example 3:
` Operations: ["initialize", "hasPrefix", "find", "add", "hasPrefix", "find"] Arguments: [[], ["word"], ["word"], ["word"], ["word"], ["word"]] Output: [null, false, false, null, true, true] Explanation: