Backtracking 1
Backtracking 1
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
}
};
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;
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(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
}
}
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>¤t,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
}
};
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
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
https://leetcode.com/tag/backtracking/discuss/1141947/Backtracking%3A-Study-and-Analysis 6/6