Skip to content

#208 - Implement Trie (Prefix Tree)

MediumTrie
Open on LeetCode

Implement a trie with insert, search, and startsWith methods.

  • insert(word) – inserts the string word into the trie.
  • search(word) – returns true if the exact string word is in the trie.
  • startsWith(prefix) – returns true if any previously inserted string has the prefix prefix.
InputOutput
OperationsOutput
insert("apple"), search("apple"), search("app"), startsWith("app"), insert("app"), search("app")[null, true, false, true, null, true]

Released under the MIT License.