π² Trie
A trie is a tree keyed on characters, giving 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 β
startsWithandsearchdiverge here.
π Study Resourcesβ
πΊ Videosβ
π Booksβ
- Algorithms β Sedgewick β Ch. 5.2 (Tries)
π Articles & Referencesβ
π» All Trie Problemsβ
Big Countries
LeetCode 595 | Difficulty: Easy
Implement Trie (Prefix Tree)
LeetCode 208 | Difficulty: Medium
Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board.