Build a prefix tree over lowercase words.
Trie()creates an empty trie.insert(word)addsword(inserting the same word twice is harmless).search(word)returnsTrueonly ifworditself was inserted.starts_with(prefix)returnsTrueif some inserted word begins withprefix.
Every operation should cost O(length of the argument), independent of how many words are stored.
t = Trie()
t.insert("card")
t.search("car") # False (only a prefix)
t.starts_with("car") # True
t.insert("car")
t.search("car") # True
t.starts_with("cat") # False
Constraints: words and prefixes have length 1..2000 and use a-z; the tests store tens of thousands of words.
Show hint
Each node needs a map from letter to child, plus a flag saying whether a word ends there: that flag is the only difference between search and starts_with.