Topic · 2 patterns
Backtracking & Recursion patterns
Backtracking builds an answer one choice at a time and undoes each choice before trying the next. Subsets, permutations and combinations enumerate every pick; general backtracking adds constraints and prunes the branches that can't lead to an answer, as in N-Queens, Sudoku and word search.
The patterns
Subsets / Permutations / Combinations
O(2ⁿ) · O(n!)
At each element choose include/exclude (subsets) or fix-then-recurse on the rest (permutations); collect at the leaves.
Spot it: "Return all subsets / combinations / permutations" with small n (≤ ~15–20); the output itself is exponential.
Backtracking
exponential
Choose → recurse → un-choose. Prune as early as the constraints allow to cut the search tree.
Spot it: "Find all valid ...", "is there a way to ...": N-Queens, Sudoku, word search, string partitions, valid parentheses, with early-checkable rules.
Learn first: Subsets / Permutations / Combinations
Which one? Match the signal
| If the problem says | Reach for |
|---|---|
| Generate ALL subsets / combinations / permutations | Subsets |
| Build a solution step by step and prune dead ends (board, word search, partition) | Backtracking |
Easy to confuse
Combination Sum IV looks like Subsets / Permutations / Combinations, but it’s Unbounded Knapsack
Only the count is asked and order matters: dp over amounts with amount as outer loop.
Word Ladder looks like Backtracking, but it’s Graph BFS / DFS
Shortest transformation sequence is BFS on an implicit graph.
Word Break looks like Backtracking, but it’s 1D / Fibonacci-style
Backtracking repeats suffixes; dp over prefix positions.
Palindrome Partitioning II looks like Backtracking, but it’s Palindromic Substrings
Only the minimum cut is needed: palindrome table + 1D cut DP.
Find the K-Sum of an Array looks like Subsets / Permutations / Combinations, but it’s K-way Merge
Enumerating subsets is 2n. Sort |a| and pop k times from a frontier heap with the 'extend / replace last' successor rule.
Tallest Billboard looks like Backtracking, but it’s 0/1 Knapsack
3n assignments collapse into dp[difference] = best shorter-side height (the editorial also shows meet-in-the-middle).
Used together
Trie (Prefix Tree) + Backtracking
When: find all dictionary words in a grid. Running word-search per word repeats the same DFS.
Backtracking + Bit Tricks & Bitmasking
When: N-Queens, Sudoku, placement puzzles. Set membership checks dominate the inner loop.
Subsets / Permutations / Combinations
- SubsetsMedium
- CombinationsMedium
Backtracking
- Generate ParenthesesMedium
- Combination Sum IIMedium
Further reading
- Complete Search with RecursionUSACO Guide
Generating subsets and permutations recursively, with problems.
- Recursion I explore cardLeetCode
Recursion fundamentals, memoization and complexity analysis.
- Recursion cheatsheetTech Interview Handbook
Base cases, stack depth and backtracking problem list.
- General approach to backtracking questionsLeetCode discuss
One template solving subsets, permutations, combination sum and partitioning.
- Competitive Programmer's HandbookAntti Laaksonen
Free, concise CP textbook; the canonical first read before CSES problems.