dsapatterns©
01Home02Patterns03Questions04
Cheatsheet
05Notes06Dashboard

© 2026 DSAPatterns

dsapatterns©
Cheatsheet
dsapatterns©
01Home02Patterns03Questions04
Cheatsheet
05Notes06Dashboard

© 2026 DSAPatterns

dsapatterns©
Cheatsheet
dsapatterns©
01Home02Patterns03Questions04
Cheatsheet
05Notes06Dashboard

© 2026 DSAPatterns

dsapatterns©
Cheatsheet
Cheatsheet›Backtracking
CORE PATTERNHigh Frequency

Backtracking

Backtracking is an algorithmic technique that explores all possible solutions by building candidates incrementally and abandoning ('backtracking from') a candidate the moment it is determined it cannot lead to a valid solution. It is a controlled form of brute-force that prunes the search space using constraints.

TimeO(N! or 2^N or N^K) — problem dependent
SpaceO(N) — recursion depth
DifficultyMedium to Hard

Key Insight

Choose, explore, un-choose — prune every dead branch early.

Flip ⟲
Backtrackingtarget = 3
exploring
found ✓
pruned ✗
010312045
start: try all subsets
Backtracking Cheatsheet
FLIP SIDEBacktracking
Tap to flip back ⟲

RECOGNITION SIGNALS (WHEN TO USE THIS PATTERN)

✓Generate all subsets, permutations, or combinations
✓Find all valid arrangements satisfying constraints
✓N-Queens, Sudoku, or grid path counting
✓Word search or sequence matching on a grid
✓Partition or split an input into valid segments
✓Return all solutions, not just one optimal value
1

Types of Problems (Subpatterns)

Each subpattern solves a specific type of problem. Click on a card to view its template.

✓ SELECTED

Subsets / Power Set

At each index, decide include or exclude — 2^N total subsets.

  • Subsets
  • Subsets II (with duplicates)
  • Letter Case Permutation
5 problems

Combinations

Choose exactly k elements — prune branches that can't reach k early.

  • Combinations
  • Combination Sum
  • Combination Sum II
5 problems

Permutations

Every ordering of elements — use a 'used' array to track which elements are in the current path.

  • Permutations
  • Permutations II (with duplicates)
  • Next Permutation
5 problems

Grid / Word Search

DFS on a 2D grid — mark cells visited in-place, restore on backtrack.

  • Word Search
  • Word Search II
  • Unique Paths III
5 problems

N-Queens / Constraint Placement

Place items row by row — use sets to check column and diagonal conflicts in O(1).

  • N-Queens
  • N-Queens II
  • Sudoku Solver
5 problems

String Partition / Palindrome

Partition a string at every valid cut — prune immediately when the prefix is invalid.

  • Palindrome Partitioning
  • Restore IP Addresses
  • Word Break II
5 problems
2

Template / Approach

Template for: Subsets / Power Set

Approach

1

Start with Empty Subset

Record the current partial subset at every call — not just at leaves. The empty set is always a valid subset.

2

Iterate from Start Index

Loop from 'start' to end of the array. Passing 'start' avoids choosing the same element twice and prevents duplicate subsets.

3

Include → Recurse → Exclude

Push the element, recurse with start = i+1, then pop the element. This is the canonical backtrack undo.

Template

</>Common Template
💡Universal subset skeleton. Record result at every node (not just leaves) to capture subsets of every size. Pass start = i+1 to avoid reusing earlier elements.
🔑
Key Idea:Record the result at every recursion node (not just leaves) to capture all subset sizes. The 'start' index enforces a forward-only scan — no element is reused. For duplicates: sort first, then skip nums[i] == nums[i-1] when i > start (same level, not a child call).
1// Subsets — include/exclude at every index
2vector<vector<int>> result;
3vector<int> current;
4
5void backtrack(vector<int>& nums, int start)
6{
7    result.push_back(current); // record subset at every node
8
9    for (int i = start; i < (int)nums.size(); i++)
10    {
11        current.push_back(nums[i]); // choose
12        backtrack(nums, i + 1); // explore (no reuse)
13        current.pop_back(); // un-choose
14    }
15}
16
17vector<vector<int>> subsets(vector<int>& nums)
18{
19    backtrack(nums, 0);
20    return result;
21}
Medium

Subsets

LeetCode #78

Medium

Subsets II

LeetCode #90

Medium

Letter Case Permutation

LeetCode #784

Medium

Find All Subsets That Sum to Target

LeetCode #

Medium

Combinations

LeetCode #77

Medium

Combination Sum

LeetCode #39

Medium

Combination Sum II

LeetCode #40

Medium

Combination Sum III

LeetCode #216

Medium

Factor Combinations

LeetCode #254

Medium

Permutations

LeetCode #46

Medium

Permutations II

LeetCode #47

Hard

Permutation Sequence

LeetCode #60

Medium

Beautiful Arrangement

LeetCode #526

Medium

Word Search

LeetCode #79

Hard

Word Search II

LeetCode #212

Hard

Unique Paths III

LeetCode #980

Hard

Sudoku Solver

LeetCode #37

Hard

N-Queens

LeetCode #51

Hard

N-Queens II

LeetCode #52

Hard

24 Game

LeetCode #679

Medium

Palindrome Partitioning

LeetCode #131

Medium

Restore IP Addresses

LeetCode #93

Medium

Generate Parentheses

LeetCode #22

Hard

Word Break II

LeetCode #140

Hard

Palindrome Partitioning II

LeetCode #132

dsapatterns

SEE THE PATTERNS. MASTER THE ALGORITHMS. ACE THE INTERVIEWS.

Visual DSA pattern library built to turn complex algorithms into clear, intuitive structures.

LEARN

  • Patterns
  • Questions
  • Notes
  • Cheatsheet

BUILD

  • Notes

© 2026 DSAPATTERNS. ALL RIGHTS RESERVED.

DESIGNED & BUILT WITH CARE

FAQsPrivacy PolicyCookies Policy