algorythms
Trie
LC #208Medium

Implement Trie (Prefix Tree)

Trie
AmazonGoogleMetaMicrosoftBloomberg

Problem

Implement a trie with insert, search (exact), and startsWith (prefix) operations.

triedesignstring

Constraints

  • 1 ≤ word.length, prefix.length ≤ 2000
  • Lowercase English letters only
  • At most 3 × 10⁴ calls total

Example

Inputinsert("apple"), search("apple"), search("app"), startsWith("app"), insert("app"), search("app")
Outputtrue, false, true, true
Why

"apple" inserted → search exact match returns true; "app" not inserted yet → false; startsWith always checks prefix

Hints — reveal one at a time