Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns

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

  1. 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.

    Learn Subsets / Permutations / CombinationsCheat sheet and practice
  2. 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

    Learn BacktrackingCheat sheet and practice

Which one? Match the signal

If the problem saysReach for
Generate ALL subsets / combinations / permutationsSubsetsO(2ⁿ)
Build a solution step by step and prune dead ends (board, word search, partition)Backtrackingexp

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.

Where to start

then the The 128 and The 365 sheets

Subsets / Permutations / Combinations

  • SubsetsMedium
  • CombinationsMedium

Backtracking

  • Generate ParenthesesMedium
  • Combination Sum IIMedium

Further reading

free, outside Lessgo
  • 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.

← Heaps & Priority QueuesAll 49 patternsTrees →
TodayReviewPatternsProblems