backtracking pruning Pattern
52 problems
Pattern pages help build reusable solving frames. Identify signals first, then explain state, transition, and edge handling.
Recognition Signals
- Do you handle empty input correctly to avoid unnecessary recursion?
- Can you explain how pruning prevents generating invalid or partial combinations?
- Checks if you handle repeated use of candidates correctly.
Solve Flow
- 1. Define the active state/window.
- 2. Update state while preserving invariants.
- 3. Validate with edge-heavy examples.
Common Misses
- Failing to prune paths when the current combination exceeds the input length, leading to unnecessary recursion.
- Failing to prune when the path sum exceeds the target, causing unnecessary recursion.
- Not sorting candidates first, which complicates duplicate skipping.
Recommended Ladder
Letter Combinations of a Phone Number
Generate all letter combinations a digit string can represent using backtracking with pruning, leveraging hash table map…
Combination Sum
Find all unique combinations of numbers from a distinct array that sum to a target using controlled backtracking search.
Combination Sum II
Find all unique combinations of numbers that sum to a target using backtracking with careful pruning to avoid duplicates…
Permutations
Generate all possible orderings of a distinct integer array using backtracking search with careful pruning to avoid dupl…
Permutations II
Generate all unique permutations of an array containing duplicates using backtracking and pruning to avoid repeated sequ…
N-Queens
Solve the N-Queens problem using backtracking with pruning, exploring all valid board placements while avoiding conflict…
N-Queens II
Solve the N-Queens II problem using backtracking with pruning to efficiently count all valid placements for n queens on …
Combinations
Generate all combinations of k numbers from the range [1, n] using backtracking and pruning.
Subsets
Generate all subsets of a set of unique integers using backtracking with pruning to avoid duplicates.
Word Search
Solve Word Search with backtracking by exploring adjacent cells to match a target word in a grid.
Gray Code
Generate an n-bit Gray Code sequence using backtracking with pruning and bit manipulation techniques.
Subsets II
Subsets II problem asks to generate unique subsets from an array with possible duplicates using backtracking search with…
Restore IP Addresses
Restore all valid IP addresses from a string using backtracking with pruning, avoiding invalid segments or leading zeros…
Word Ladder II
Find all shortest transformation sequences from beginWord to endWord using a dictionary, leveraging backtracking search …
Word Search II
Solve the Word Search II problem using backtracking with pruning to find all words on a board of characters.
Combination Sum III
Find all unique combinations of k numbers adding to n using efficient backtracking with pruning in arrays.
Expression Add Operators
Expression Add Operators is solved with backtracking that builds multi-digit operands and tracks multiplication preceden…
Remove Invalid Parentheses
Remove the minimum number of invalid parentheses to generate all possible valid strings efficiently using backtracking a…
Additive Number
Additive Number is solved by trying the first two splits, then pruning aggressively while verifying each required sum in…
Binary Watch
Binary Watch involves generating possible times on a binary watch given a number of turned-on LEDs.
24 Game
Solve the 24 Game by arranging four card numbers using arithmetic operators and parentheses to reach exactly 24 efficien…
Letter Case Permutation
Letter Case Permutation involves generating all possible strings by changing the case of each letter in a given string.
Ambiguous Coordinates
Find all valid 2D coordinate possibilities for an ambiguous input string using backtracking and pruning techniques.
Split Array into Fibonacci Sequence
This problem challenges you to split a string into a Fibonacci-like sequence using backtracking and pruning.
Largest Time for Given Digits
Given four digits, determine the latest valid 24-hour time possible using each digit exactly once with backtracking.
Numbers With Same Consecutive Differences
Generate all n-digit numbers where consecutive digits differ by k using efficient backtracking and BFS techniques.
Unique Paths III
Solve the Unique Paths III problem using backtracking search with pruning to count 4-directional paths covering all empt…
Letter Tile Possibilities
Compute all unique non-empty sequences from given letter tiles using backtracking search with pruning efficiently.
Path with Maximum Gold
Find the path with the maximum gold in a grid with backtracking and pruning techniques to maximize the gold collected.
Circular Permutation in Binary Representation
Generate a circular permutation from a range of numbers using backtracking and bit manipulation.
Maximum Length of a Concatenated String with Unique Characters
Find the maximum length of a concatenated string from an array with all unique characters using backtracking search with…
Tiling a Rectangle with the Fewest Squares
Tiling a Rectangle with the Fewest Squares problem asks for the minimum number of squares required to cover a rectangle …
Iterator for Combination
Implement an iterator that generates all combinations of a given length using efficient backtracking with pruning.
Verbal Arithmetic Puzzle
Check if a verbal arithmetic equation can be solved using a valid digit-letter mapping.
The k-th Lexicographical String of All Happy Strings of Length n
Given n and k, find the k-th lexicographical happy string of length n using backtracking search with pruning.
Split a String Into the Max Number of Unique Substrings
Maximize unique substrings in a string using backtracking with pruning and hash tables to track substrings.
Maximum Number of Achievable Transfer Requests
Find the maximum number of achievable transfer requests between buildings with constraints on net employee transfers.
Construct the Lexicographically Largest Valid Sequence
Construct the Lexicographically Largest Valid Sequence problem involves finding the largest sequence with backtracking a…
Splitting a String Into Descending Consecutive Values
Check if a string of digits can be split into descending consecutive values where the difference between them is 1.
Sum of All Subset XOR Totals
Compute the sum of all subset XOR totals using a backtracking search with pruning for arrays of small size.
Longest Subsequence Repeated k Times
Find the longest subsequence repeated k times in a string using backtracking search with pruning.
Count Number of Maximum Bitwise-OR Subsets
Determine the number of non-empty subsets that achieve the maximum bitwise OR using efficient backtracking and pruning s…
Next Greater Numerically Balanced Number
Find the smallest numerically balanced number greater than a given integer using backtracking search and pruning.
Number of Valid Move Combinations On Chessboard
Given a set of pieces on a chessboard, calculate the number of valid move combinations without overlap using backtrackin…
Maximum Path Quality of a Graph
Calculate the maximum path quality in a graph using backtracking and pruning to optimize node visits within time limits …
Maximum Good People Based on Statements
Determine the maximum number of good people in a group given mutual statements, using precise backtracking with pruning.
Maximum Split of Positive Even Integers
Determine the largest set of unique positive even integers that sum to a given finalSum using backtracking and greedy se…
Maximum Points in an Archery Competition
In the "Maximum Points in an Archery Competition" problem, Bob aims to maximize his score while respecting Alice's arrow…
Maximum Rows Covered by Columns
Select exactly numSelect columns from a binary matrix to maximize rows where all ones are covered efficiently using back…
Find the Punishment Number of an Integer
Find the punishment number of a given integer using backtracking search with pruning.
Generate Binary Strings Without Adjacent Zeros
Generate all binary strings of length n without adjacent zeros using backtracking search with pruning for efficient enum…
Smallest Divisible Digit Product II
Find the smallest zero-free number at least as large as num whose digits multiply to a product divisible by t using care…