0% found this document useful (0 votes)
11 views6 pages

Backtracking 1

The document discusses backtracking in coding, highlighting common problems and providing code examples for each. It includes recommendations for practicing specific backtracking problems on LeetCode, such as generating parentheses and permutations. The author aims to simplify the understanding of backtracking concepts and encourages readers to engage with the material.

Uploaded by

Kamal Kannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Backtracking 1

The document discusses backtracking in coding, highlighting common problems and providing code examples for each. It includes recommendations for practicing specific backtracking problems on LeetCode, such as generating parentheses and permutations. The author aims to simplify the understanding of backtracking concepts and encourages readers to engage with the material.

Uploaded by

Kamal Kannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

26/03/2022, 22:52 (1) Backtracking: Study and Analysis - LeetCode Discuss

Premium
🔈 LeetCode is hiring!
(/) Explore(/explore/) Problems(/problemset/all/) Interview Contest Discuss(/discuss/) Apply NOW.🔈
Store(https://leetcode.com/jobs/)(/subscribe?
New (/contest/) 1

ref=nb_npl)
 Backtracking
Problems Discuss
Back(/tag/backtracking/discuss) Backtracking: Study and Analysis
(/thisisakshat) thisisakshat (/thisisakshat) 736 Last Edit: April 4, 2021 3:33 PM 2.6K VIEWS
87 Hey everyone , I hope you all are doing fine and are healthy. I have struggled with Backtracking for the better part of my coding , cause
even though the answers are available, and resources are available, no one that I found was willling enough to explain the intuition and
concept behind it, and to judge the method to do it.
Here , in this post, I am going to try and post some questions related to B.T. and below that I will share the code and a link. The link is for
those who would like to see a detailed solution, and they can visit that. I want to keep this article short, so that those looking for the
answer directly don't have to go past all the explanation part for each question, while also not compromising others who want to see a
detail analysis .
So, lets get right into it.
I.) Backtracking problems: There are multiple problems available on LC for practicing B.T., but these are the ones I would like to
recommend to go through first before handling others:
1.) Letter Combinations of a Phone Number (https://leetcode.com/problems/letter-combinations-of-a-phone-number)
2.)Generate Parentheses (https://leetcode.com/problems/generate-parentheses)
3.)Permutations (https://leetcode.com/problems/permutations)
4.)Permutations II (https://leetcode.com/problems/permutations-ii)
5.)Combinations (https://leetcode.com/problems/combinations)
6.)Subsets (https://leetcode.com/problems/subsets)
7.)Subsets II (https://leetcode.com/problems/subsets-ii)
8.)N-Queens (https://leetcode.com/problems/n-queens)
II.)Solutions:
Letter Combinations of a Phone Number :
Code:
class Solution {
public:
vector<string>result;
void helper(string digits,int index,string current)
{
vector<string>v= {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; // to generat
e string for that index
if(index==digits.length()) //base case
{
result.push_back(current);
return;
}
string s=v[digits[index]-'0'];
for(int i=0;i<s.length();i++)
{
// If some letter remaining to consider, consider that and generate the combination or go to bas
e case and end recursion for that letter
helper(digits,index+1,current+s[i]);
}
}
vector<string> letterCombinations(string digits) {
if(digits=="") return {};
helper(digits,0,"");
return result; //return answer
}
};

For Detail Solution: Solution (https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/1139938/Clear-and-


simple-explanation-with-intuition%3A-100-faster)
Generate Parentheses
Code:

https://leetcode.com/tag/backtracking/discuss/1141947/Backtracking%3A-Study-and-Analysis 1/6
26/03/2022, 22:52 (1) Backtracking: Study and Analysis - LeetCode Discuss

class Solution {
public:
vector<string>result;

void helper(int open,int close,int n,string current)


{
if(current.length()==n*2) // base case
{
result.push_back(current);
return;
}
// If opening are less than n, hence generate combination with one more opening
if(open<n) helper(open+1,close,n,current+"(");
// If closing are less than n, hence generate combination with one more closing as conditions are met
if(close<open) helper(open,close+1,n,current+")");
}
vector<string> generateParenthesis(int n) {
helper(0,0,n,"");
return result;
}
};

For Detail Solution: Solution (https://leetcode.com/problems/generate-parentheses/discuss/1131364/Clear-and-simple-explanation-with-


intuition%3A-100-faster)
Permutations
Code:
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> output; // to hold result
generatePermutations(nums, output, 0);
return output; // return answer
}
private:
void generatePermutations(vector<int> nums, vector<vector<int>>&output, int idx) {
if (idx == size(nums)) // base case
{
output.emplace_back(nums);
}
for (int i = idx; i < size(nums); ++i)
{
swap(nums[i], nums[idx]); // swap the current element with the next to get another permutation
generatePermutations(nums, output, idx + 1); // generate permutations for the next element
}
}
};

For Detail Solution:Solution (https://leetcode.com/problems/permutations/discuss/1140113/Clear-and-simple-explanation-(2-approach)-


with-intuition%3A-100-faster)
Permutations II
Code:

https://leetcode.com/tag/backtracking/discuss/1141947/Backtracking%3A-Study-and-Analysis 2/6
26/03/2022, 22:52 (1) Backtracking: Study and Analysis - LeetCode Discuss

class Solution {
public:
vector<vector<int>>ans;
void helper(vector<int> nums,int idx)
{
if(idx==nums.size()) // base case
{
ans.push_back(nums);
}
for(int i=idx;i<nums.size();i++)
{
if(i!=idx && nums[i]==nums[idx]) continue;
swap(nums[i],nums[idx]);
helper(nums,idx+1);
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(),nums.end());
helper(nums, 0);
return ans;
}
};

For Detail Solution:Solution (https://leetcode.com/problems/permutations-ii/discuss/1140136/C%2B%2B-Simple-and-efficient-or-2-


different-solutions)
Combinations
Code:
class Solution {
public:
vector<vector<int>> ans;

void helper(int idx, int k,vector<int>&current,int n)


{
if(current.size()==k) // base case
{
ans.push_back(current);
return;
}

for(int i=idx;i<n+1;i++)
{
current.push_back(i); //consider the current element i
helper(i+1,k,current,n); // generate combinations
current.pop_back(); //proceed to next element
}
}

vector<vector<int>> combine(int n, int k) {


vector<int>current;
helper(1,k,current,n);
return ans; //return answer
}
};

For Detail Solution: Solution (https://leetcode.com/problems/combinations/discuss/1141903/Clear-and-simple-explanation-with-


intuition%3A-100-faster)
Subsets
Code:

https://leetcode.com/tag/backtracking/discuss/1141947/Backtracking%3A-Study-and-Analysis 3/6
26/03/2022, 22:52 (1) Backtracking: Study and Analysis - LeetCode Discuss

class Solution {
public:
vector<vector<int>>subset;
void helper(int index, vector<int>&current,vector<int>&nums)
{
subset.push_back(current); // push the current subset into the resultant array
for(int i=index;i<nums.size();i++)
{
current.push_back(nums[i]); // add the current element to consider the subsets corresponding to i
t
helper(i+1,current,nums); //generate subsets for this array
current.pop_back(); // as this has been used, pop it
}
return;
}
vector<vector<int>> subsets(vector<int>& nums) {
vector<int>current;
helper(0,current,nums);
return subset; //return answer
}
};

For Detail Solution: Solution (https://leetcode.com/problems/subsets/discuss/1140388/Clear-and-simple-explanation-with-example-


%3A-100-faster)
Subsets II
Code:
class Solution {
public:
set<vector<int>>subsets; // initialising set
void helper(int index,vector<int>&current, vector<int>&nums)
{
subsets.insert(current); //inserting current array to the result
for(int i=index;i<nums.size();i++)
{
current.push_back(nums[i]); // add the current element to consider the subsets corresponding to i
t
helper(i+1,current,nums); //generate subsets for this array
current.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<int>current;
helper(0,current,nums);
vector<vector<int>>ans(subsets.begin(),subsets.end()); //make vector from set
return ans; //return answer
}
};

For Detail Solution: Solution (https://leetcode.com/problems/subsets-ii/discuss/1140408/Clear-and-simple-explanation-with-intuition)


N-Queens
Code:

https://leetcode.com/tag/backtracking/discuss/1141947/Backtracking%3A-Study-and-Analysis 4/6
26/03/2022, 22:52 (1) Backtracking: Study and Analysis - LeetCode Discuss

class Solution {
public:
vector<vector<string>>ans;

bool check(int row,int col,int n,vector<string>&curr) // A fn to check if a queen can be placed or not in
a partcular row
{
for(int i=0;i<row;i++) //check the column
{
if(curr[i][col]=='Q')
return false;
}
for(int i=row-1,j=col-1;i>=0 && j>=0;i--,j--) //check left diagonal
{
if(curr[i][j]=='Q')
return false;
}
for(int i=row-1,j=col+1;i>=0 && j>=0;i--,j++) //check right diagonal
{
if(curr[i][j]=='Q')
return false;
}
// If there is no queen placed in left, or right diagonal or in the same column, then place the quee
n by returning true
return true;
}
void helper(vector<string>& curr,int n, int row) // to generate all the possible combinations
{
if(row==n) // if we have reached the end for a particular column, push the current combination into t
he result and return
{
ans.push_back(curr);
return;
}
for(int col=0;col<n;col++) // We iterate over column and hence row ,left and right diagonal remain
to be considered which we consider in check function
{
if(check(row,col,n,curr)) // If it can be placed
{
curr[row][col]='Q'; // place the queen in that particular row and column
helper(curr,n,row+1); // check the next row for placement
curr[row][col]='.'; //reset it for future column and row combinations
}
}
return;
}
vector<vector<string>> solveNQueens(int n) {
vector<string>curr(n,string(n,'.'));
helper(curr,n,0);
return ans; //return answer
}
};

I hope you liked this post. I only considered doing it for 8 questions since I didn't know if this helps or not.
If it helped, please UPVOTE .
Also, I would keep updating this with more and more problems, if this turns out to be helpful .
Happy Coding and keep up the good work :)
backtracking easy

Comments: 1 Best Most Votes Newest to Oldest Oldest to Newest

Type comment here... (Markdown is supported)

Post
(/SAHIL_BAZARD) SAHIL_BAZARD (/SAHIL_BAZARD) 0 February 13, 2022 7:55 AM
Thanks for taking your time and combining major backtracking problems in one article.
0 Reply

https://leetcode.com/tag/backtracking/discuss/1141947/Backtracking%3A-Study-and-Analysis 5/6
26/03/2022, 22:52 (1) Backtracking: Study and Analysis - LeetCode Discuss

Copyright © 2022 LeetCode


Help Center (/support) Jobs (/jobs) Bug Bounty (/bugbounty) Online Interview (/interview/) Students (/student) Terms (/terms) Privacy Policy (/privacy)
United States (/region)

https://leetcode.com/tag/backtracking/discuss/1141947/Backtracking%3A-Study-and-Analysis 6/6

You might also like