AlgoMaster Newsletter

AlgoMaster Newsletter

Share this post

AlgoMaster Newsletter
AlgoMaster Newsletter
LeetCode Pattern - Backtracking
Copy link
Facebook
Email
Notes
More
User's avatar
Discover more from AlgoMaster Newsletter
Master Coding and System Design Interviews. Level up your Software Engineering career. Subscribe and get a FREE System Design Interview Handbook in your inbox.
Over 95,000 subscribers
Already have an account? Sign in

LeetCode Pattern - Backtracking

Ashish Pratap Singh's avatar
Ashish Pratap Singh
Jun 09, 2025

Share this post

AlgoMaster Newsletter
AlgoMaster Newsletter
LeetCode Pattern - Backtracking
Copy link
Facebook
Email
Notes
More
Share

Backtracking is an algorithmic paradigm that tries different solutions until finding a solution that works.

Backtracking is a depth-first search (DFS) algorithm that builds solutions incrementally.

It involves choosing a candidate, exploring further by recursively calling the function, and backtracking if the candidate leads to a dead end.

This technique is often used for problems involving permutations, combinations, and subsets.

Backtracking is particularly useful for:

  1. Finding all (or some) solutions to a problem

  2. Optimization problems (finding the best solution)

  3. Decision problems (finding if a solution exists)

Basic Operations in Backtracking

The backtracking algorithm typically follows this pattern:

  1. Choose: Select a candidate from the set of possible options.

  2. Constraint: Check if the choice satisfies all constraints.

  3. Goal: Check if the current state is a valid solution.

  4. Recurse: If the constraints are satisfied but the goal isn't reached, make a recursive call.

  5. Backtrack: Remove the last chosen candidate if it leads to a dead end and try another candidate.

Implementing Backtracking

Here's a general template for implementing backtracking:

LeetCode Examples

1. Subsets (LeetCode 78)

Problem Statement: Given a set of distinct integers, return all possible subsets (the power set).

Intuition:

To generate all subsets, we can use backtracking to explore each possibility by either including or excluding each element in the set.

Approach:

  1. Start with an empty subset.

  2. Use backtracking to recursively add elements to the current subset and explore further.

  3. At each step, add the current subset to the result list.

  4. Backtrack by removing the last added element and try the next candidate.

Code:

This solution generates all subsets by recursively including or excluding each element.

Subscribe to receive new articles every week.

2. Permutations (LeetCode 46)

Problem Statement: Given a collection of distinct integers, return all possible permutations.

Intuition:

To generate all permutations, we can use backtracking to swap elements and explore each possibility.

Approach:

  1. Start with the first element.

  2. Use backtracking to recursively swap the current element with each subsequent element and explore further.

  3. Add the current permutation to the result list when all elements are fixed.

  4. Backtrack by swapping the elements back to their original positions.

This solution generates all permutations by swapping elements and recursively permuting the rest.

3: Combination Sum

Problem Statement: Given an array of distinct integers candidates and a target integer target, return all unique combinations in candidates where the candidate numbers sum to target. Each number in candidates may be used an unlimited number of times.

Intuition:

To find all combinations that sum to the target, we can use backtracking to explore each possibility by either including or excluding each element multiple times.

Approach:

  1. Start with an empty combination.

  2. Use backtracking to recursively add elements to the current combination and explore further.

  3. Add the current combination to the result list if the sum equals the target.

  4. Backtrack by removing the last added element and try the next candidate.

Advanced Techniques

  1. Pruning: Optimize backtracking by early termination of paths that cannot lead to a valid solution.

  2. State Management: Efficiently manage state to avoid unnecessary copying of data structures.

  3. Iterative Backtracking: Implement backtracking iteratively using a stack for better space efficiency in some cases.

  4. Bit Manipulation: Use bit manipulation techniques to optimize certain backtracking problems, especially for set operations.

Common Pitfalls and Tips

  1. Infinite Recursion: Ensure your base case is correctly defined to avoid infinite recursion.

  2. Modifying Shared State: Be careful when modifying shared state in recursive calls. Always revert changes when backtracking.

  3. Unnecessary Work: Look for opportunities to prune the search space early.

  4. Duplicate Solutions: In problems where uniqueness matters, consider using sets or sorting to eliminate duplicates.

Time and Space Complexity

The time complexity of backtracking algorithms can often be exponential, as they explore all possible combinations.

However, effective pruning can significantly reduce the actual running time.

Space complexity is typically O(n) for the recursion stack, where n is the depth of the recursion tree.

Conclusion

The Backtracking pattern is a powerful technique for solving complex combinatorial problems. Key points to remember:

  1. Backtracking is about systematically trying out all possibilities.

  2. It's particularly useful for problems involving permutations, combinations, and constraint satisfaction.

  3. The core idea is to build the solution incrementally and abandon a path as soon as it's determined to be invalid.

  4. Effective pruning of the search space is crucial for efficiency.


Thank you so much for reading.

If you found it valuable, hit a like ❤️ and consider subscribing for more such content every week.

If you have any questions or suggestions, leave a comment.

This post is public so feel free to share it.

Share

Subscribe for free to receive new posts and support my work.

Checkout my Youtube channel for more in-depth content.

Follow me on LinkedIn and X to stay updated.

Checkout my GitHub repositories for free interview preparation resources.

I hope you have a lovely day!

See you soon,

Ashish

Share this post

AlgoMaster Newsletter
AlgoMaster Newsletter
LeetCode Pattern - Backtracking
Copy link
Facebook
Email
Notes
More
Share
© 2025 Ashish Pratap Singh
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More