Skip to main content

🌲 Trie

A trie is a tree keyed on characters, giving O(L)O(L) prefix lookups. Useful for autocomplete, word searches, and any "prefix" question. Often combined with backtracking on grids.

This category contains 3 problems. Use the patterns below to recognize what's being asked, then jump to the problem list at the bottom.


🧠 Key Patterns​

  • Insert / Search / StartsWith β€” The base operations every trie problem builds on.
  • Word Search II β€” Trie + DFS in a grid β€” bulk match many words in one pass.
  • Replace Words / Longest Common Prefix β€” Walk the trie until you fall off.
  • Suffix Trie / Suffix Array β€” Substring queries on a single string.
  • XOR Maximum / Bit-Trie β€” Trie keyed on bits for max-XOR pair problems.

⚠️ Common Pitfalls​

  • Memory: each node has up to 26 (or 128) children. For huge dictionaries prefer a hash map per node.
  • Forgetting the "end of word" flag β€” startsWith and search diverge here.

πŸ“š Study Resources​

πŸ“Ί Videos​

πŸ“– Books​

  • Algorithms β€” Sedgewick β€” Ch. 5.2 (Tries)

🌐 Articles & References​


πŸ’» All Trie Problems​