When to Use

Pattern triggers

Look for these signals in the problem:

  • "Prefix" search or autocomplete: Trie finds all words with given prefix efficiently.
  • "Word dictionary" with insert/search: Trie handles both operations in O(L)O(L) time.
  • "Wildcard" matching (like '.'): Trie enables DFS exploration at wildcard positions.
  • "Word search" in grid with dictionary: Trie prunes paths not matching any word prefix.
  • "Longest common prefix": Traverse trie until branching to find shared prefix. Uses O(total characters)O(\text{total characters}) space.