11
Backtracking & Subsets
Combinations, permutations and subsets.
5 pages
- Pattern 11: Backtracking / SubsetsPattern guide
Every backtracking problem is choose → explore → un-choose. The only thing that varies is the loop's start index and the pruning condition.
- LC 46Permutations (LC 46)Medium
Build every ordering of distinct elements by fixing one position at a time and exploring choices, then undo (backtrack).
- LC 77Combinations (LC 77)Medium
Choose exactly k distinct numbers from 1..n without caring about order — build increasing sequences and prune when too few numbers remain.
- LC 39Combination Sum (LC 39)Medium
Build sums with unlimited reuse of each candidate; after including candidates[i], recurse with i (not i+1) so the same value can be picked again.
- LC 22Generate Parentheses (LC 22)Medium
Only extend strings that can still become valid: add '(' while open < n, add ')' while close < open.