All Patterns

Trie

Build prefix trees for efficient string operations like autocomplete, prefix matching, and word search.

When to use

  • Prefix matching/autocomplete
  • Word dictionary operations
  • Finding common prefixes
  • Word search in grid

When NOT to use

  • Single string operations
  • Exact match only (use hash map)

Common traps

  • Not marking end-of-word nodes
  • Memory overhead for sparse tries
  • Confusing trie traversal with tree traversal

Key Invariant

Each path from root to a marked node represents a stored word — shared prefixes share nodes

Problems (10)

#120Implement Trie (Prefix Tree)
medium
#122Design Add and Search Words Data Structure
medium
#123Longest Word in Dictionary
medium
#124Replace Words
medium
#125Map Sum Pairs
medium
#126Maximum XOR of Two Numbers in an Array
medium
#127Search Suggestions System
medium
#298Maximum XOR of Two Numbers
medium
#301KMP Algorithm
medium
#121Word Search II
hard