Backtracking 2
Backtracking 2
Premium
(/) Explore(/explore/) Problems(/problemset/all/) InterviewNew Contest 🔈 LeetCode is hiring! Apply NOW.🔈
Store(https://leetcode.com/jobs/)(/subscribe?
(/contest/)
Discuss(/discuss/) 1
ref=nb_npl)
Backtracking
Problems Discuss
Back(/tag/backtracking/discuss) Backtracking algorithm + problems to practice
(/A1exrebys) A1exrebys (/A1exrebys) 359 Last Edit: August 16, 2021 7:03 AM 3.3K VIEWS
39 Hi folks! I created a list of backtracking problems that can be useful to practice to solve more problems during interview / contest.
Backtracking algorithm is straightforward, but when it comes to real problems sometimes it is not obvious how we should tweak the
algorithm.
Let's check the basic description and template of backtracking algorithm:
Wikipedia: Backtracking is a general algorithm for finding solutions to some computational problems, notably
constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate
("backtracks") as soon as it determines that the candidate cannot possibly be completed to a valid solution.
void dfs(arguments) {
if (condition == true) { // Condition when we should stop our exploration.
result.push_back(current);
return;
}
for (int i = num; i <= last; i++) {
current.push_back(i); // Explore candidate.
dfs(arguments);
current.pop_back(); // Abandon candidate.
}
}
You might notice that we change the original template for a bit:
1. We stop our exploration if the current array size is equal to the input array size, and we add current array to our result set.
2. As input array consist of district elements we can use unordered_set to track if this element has used before.
3. Backtrack: when we explore element we should remove it from vector and unordered_set.
Note: we can also use swap(), but it is easier to show the pattern using unordered_set.
https://leetcode.com/tag/backtracking/discuss/1405817/Backtracking-algorithm-%2B-problems-to-practice 1/4
26/03/2022, 22:52 (1) Backtracking algorithm + problems to practice - LeetCode Discuss
Another variation of the problem is Permutations II (https://leetcode.com/problems/permutations-ii/).
The only difference between first problem is that we MAY have DUPLICATES in the input array.
void dfs(vector& nums, vector& cur, vector>& res,
unordered_map& hmap) {
if (cur.size() == nums.size()) { // (1)
res.push_back(cur);
return;
}
for (auto& [num, freq] : hmap) { // (2)
if (freq == 0) continue; // (3)
freq--;
cur.push_back(num);
dfs(nums, cur, res, hmap);
cur.pop_back(); // (4)
freq++;
}
}
https://leetcode.com/tag/backtracking/discuss/1405817/Backtracking-algorithm-%2B-problems-to-practice 2/4
26/03/2022, 22:52 (1) Backtracking algorithm + problems to practice - LeetCode Discuss
Let's check for is the difference between others examples:
1. Here we don't need to check the current array size. If target result is equal to 0 then it means that we find a valid combination and
can add it to the current result set.
2. Almost the same as in earlier example, but we will be iterating untill we reach the end of the candidates.
3. Remember that we sort candidates array. One trick here i > ind is needed because we are only allowed to use each number in
candidates once.
4. Backtrack: the same as in earlier examples.
Subsets: we are given an integer array of unique elements, return all possible subsets (the power set). If you want to solve this
problem before check the solution, here is the link: Subsets (https://leetcode.com/problems/subsets/)
void dfs(int ind, vector& nums, vector& cur, vector>& res) {
res.push_back(cur); // (1)
for (int i = ind; i < nums.size(); i++) { // (2)
cur.push_back(nums[i]);
dfs(i + 1, nums, cur, res);
cur.pop_back(); // (3)
}
}
For the last problem in this article let's check more complex / interesting problem that can be solved using backtracking: Word Squares
(https://leetcode.com/problems/word-squares/). We are given an array of unique strings words, we need to return all the word squares
you can build from words. Note: result word square should be symmetrical across its diagonal.
vector> wordSquares(vector& words) {
vector<vector<string>> res;
vector<string> cur;
unordered_map> hmap;
for (auto& w : words) {
for (int i = 0; i < w.size(); i++) hmap[w.substr(0, i)].push_back(w); // (1)
}
dfs(0, words[0].size(), res, cur, hmap);
return res;
}
void dfs(int ind, int sz, vector>& res, vector& cur,
unordered_map>& hmap) {
if (ind == sz) { // (2)
res.push_back(cur);
return;
}
string pref;
for (int i = 0; i < ind; i++) pref += cur[i][ind]; // (3)
for (auto& w : hmap[pref]) { // (4)
cur.push_back(w);
dfs(ind + 1, sz, res, cur, hmap);
cur.pop_back(); // (5)
}
}
The dfs() algorithm here is almost the same, but let's check the key points:
1. Prerequisite: we will use unordered_map to generate prefixes for each word before the first call of dfs().
2. If our current vector of strings has the same size as original list of words, then we should add it to our result set and stop exploring.
https://leetcode.com/tag/backtracking/discuss/1405817/Backtracking-algorithm-%2B-problems-to-practice 3/4
26/03/2022, 22:52 (1) Backtracking algorithm + problems to practice - LeetCode Discuss
3. One extra step compared to all others examples is that we are generating the prefix that is needed for the next word to satisfy the
problem requirement.
4. Iterating through the possible candidates that starts with the given prefix.
5. Backtrack: the same as for others problems.
Links to the problems above + some more problems to practice:
1. Permutations: https://leetcode.com/problems/permutations/ (https://leetcode.com/problems/permutations/)
2. Permutations II: https://leetcode.com/problems/permutations-ii/ (https://leetcode.com/problems/permutations-ii/)
3. Combinations: https://leetcode.com/problems/combinations/ (https://leetcode.com/problems/combinations/)
4. Combination Sum: https://leetcode.com/problems/combination-sum/ (https://leetcode.com/problems/combination-sum/)
5. Combination Sum II: https://leetcode.com/problems/combination-sum-ii/submissions/ (https://leetcode.com/problems/combination-
sum-ii/submissions/)
6. Combination Sum III: https://leetcode.com/problems/combination-sum-iii/ (https://leetcode.com/problems/combination-sum-iii/)
7. Subsets: https://leetcode.com/problems/subsets/submissions/ (https://leetcode.com/problems/subsets/submissions/)
8. Subsets II: https://leetcode.com/problems/subsets-ii/submissions/ (https://leetcode.com/problems/subsets-ii/submissions/)
9. Palindrome Partitioning: https://leetcode.com/problems/palindrome-partitioning/submissions/
(https://leetcode.com/problems/palindrome-partitioning/submissions/)
10. Generate parenthesis: https://leetcode.com/problems/generate-parentheses/ (https://leetcode.com/problems/generate-
parentheses/)
11. Letter Combinations: https://leetcode.com/problems/letter-combinations-of-a-phone-number/solution/
(https://leetcode.com/problems/letter-combinations-of-a-phone-number/solution/)
12. Word search: https://leetcode.com/problems/word-search/ (https://leetcode.com/problems/word-search/)
13. N-Queens: https://leetcode.com/problems/n-queens/ (https://leetcode.com/problems/n-queens/)
14. Sudoku solver: https://leetcode.com/problems/sudoku-solver/ (https://leetcode.com/problems/sudoku-solver/)
15. Word Squares: https://leetcode.com/problems/word-squares/ (https://leetcode.com/problems/word-squares/)
backtracking c++
Post
(/hX_) hX_ (/hX_) 346 November 9, 2021 7:05 AM
Can you please explain or share some resources on how to calculate time complexity and space complexity of backtracking problems??
Thanks
3 Reply
(/suryakantpandey) suryakantpandey (/suryakantpandey) 3 Last Edit: August 27, 2021 9:37 AM
https://leetcode.com/list/9x0zfcir (https://leetcode.com/list/9x0zfcir) here is the link for list and it doesn't contain premium questions
2 Show 1 reply Reply
(/Symbolistic) Symbolistic (/Symbolistic) 33 December 2, 2021 9:46 AM
I just wanna say how much I appreciate you making this post, it helped make backtracking "click" for me in my head finally.
Thank you
1 Reply
(/rudeTool) rudeTool (/rudeTool) 46 August 23, 2021 8:23 PM
Can you create a list and share it?
1 Reply
https://leetcode.com/tag/backtracking/discuss/1405817/Backtracking-algorithm-%2B-problems-to-practice 4/4