Topic 09 of 14

Backtracking Patterns

3 patterns and 15 practice problems, each with a recall tip, a diagram, the theory and a Python template.

01

Backtracking - Subsets / Combinations

5 practice problems

Theory

  • For each element, decide to include it or not — generates all subsets.
  • For combinations of size k: only recurse until path has k elements.
  • To avoid duplicates with duplicate input: sort first, skip equal adjacent element at same level.
  • Time: O(2^n) for subsets, O(C(n,k)) for combinations.

Code template

python
def subsets(nums):
    result = []
    def backtrack(start, path):
        result.append(path[:])
        for i in range(start, len(nums)):
            path.append(nums[i])
            backtrack(i + 1, path)
            path.pop()
    backtrack(0, [])
    return result

def combine(n, k):
    result = []
    def backtrack(start, path):
        if len(path) == k:
            result.append(path[:])
            return
        for i in range(start, n + 1):
            path.append(i)
            backtrack(i + 1, path)
            path.pop()
    backtrack(1, [])
    return result

Practice problems · 5

Medium
Medium
Medium
Medium
02

Backtracking - Permutations

5 practice problems

Theory

  • Every element must appear exactly once. Use a visited set or swap-based approach.
  • For duplicates: sort first, only pick an element if the previous duplicate was used in this path.
  • Number of permutations: n! — grows fast, prune whenever possible.
  • Swap-based: swap element into current position, recurse, swap back.

Code template

python
def permutations(nums):
    result = []
    def backtrack(path, used):
        if len(path) == len(nums):
            result.append(path[:])
            return
        for i in range(len(nums)):
            if used[i]:
                continue
            used[i] = True
            path.append(nums[i])
            backtrack(path, used)
            path.pop()
            used[i] = False
    backtrack([], [False] * len(nums))
    return result

Practice problems · 5

03

Backtracking - Grid / Board Search

5 practice problems

Theory

  • DFS from each cell. Mark visited to avoid revisiting in the current path.
  • Unmark (backtrack) when returning so other paths can use the cell.
  • Prune by checking bounds, visited status, and character match before recursing.
  • Time: O(m * n * 4^L) for word search of length L on an m × n board.

Code template

python
def word_search(board, word):
    rows, cols = len(board), len(board[0])
    def dfs(r, c, idx):
        if idx == len(word):
            return True
        if (r < 0 or r >= rows or c < 0 or c >= cols
                or board[r][c] != word[idx]):
            return False
        temp = board[r][c]
        board[r][c] = '#'  # mark visited
        found = (dfs(r+1,c,idx+1) or dfs(r-1,c,idx+1)
                 or dfs(r,c+1,idx+1) or dfs(r,c-1,idx+1))
        board[r][c] = temp  # backtrack
        return found
    for r in range(rows):
        for c in range(cols):
            if dfs(r, c, 0):
                return True
    return False

Practice problems · 5

Medium
Hard
Hard
Hard

Spotted a mistake on this page?

Wrong formula, broken animation or confusing wording. Tell us and we will fix it.