Backtracking Part 3 — Permutations Problems

QUESTION 1 — Permutations

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

Example:

I will discuss 2 different thought processes and solutions for this question. Both approaches involve Backtracking.

THOUGHT PROCESS FOR BACKTRACKING SOLUTION — 1

Let's revise the backtracking Brahmastr before moving on, as we will follow it anyways in the approach.

The Backtracking Brahmastra :

  1. Find what choice(s) we have at each step. What different options are there for the next step?
  2. For each valid choice:

(a)Make it and explore recursively. Pass the information for a choice to the next recursive call(s).

(b)Undo it after exploring. Restore everything to the way it was before making this choice.

3. Find our base case(s). What should we do when we are out of decisions?

Let’s fit in our question :

  1. Find what choice(s) we have at each step.

In the currans to include any of the n available numbers. Let’s take an example and try to understand, What I mean by saying “having the choice to include any of the n available numbers”. Let’s say input = [1,2,3], currently the currans is {}, Choice to add to currans — 1,2, or 3,

2. For each valid choice:

(a)Make it and explore recursively. Pass the information for a choice to the next recursive call(s).

Make the choice of including 1, 2 or 3 in the currans, with this line currans.push_back(nums[i])

Explore recursively helper(nums,currans) i.,e explore all the possible answers starting with ith choice, let's say i = 0, nums[i] = 1, so this call is meant to explore all the permutations starting with 1, so when you return from this call, you can expect that recursion would have added [1,2,3], [1,3,2] in your final answer.

(b)Undo it after exploring. Restore everything to the way it was before making this choice.

Now it’s time to revoke the previous choice and restore things to as they were before making this particular choice. Hence, currans.pop_back()

3. Find our base case(s). This one should be an easy guess if the currans’ size is equal to input’s size, we can conclude we made a permutation and push it back into our answer and return from the call.

Typical decision tree (pardon for the poor handwriting)

THOUGHT PROCESS FOR BACKTRACKING SOLUTION — 2

The idea is to swap each of the remaining characters in the string with its first character and then find all the permutations of the remaining characters using a recursive call. The base case of the recursion is when the string is left with only one unprocessed element. Below is the recursion tree for printing all permutations of string “ABC”

QUESTION 2 — Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

Example:

The core idea of the solution is gonna be the same as the 1st solution that we discussed above. We just need to take care that we don’t make any duplicate recursive calls, for which we use a boolean array used(I know I have used an integer vector) if the ith number has already been pushed into currans i.e, if used[i] = 1 or if nums[i] == nums[i-1] and nums[i-1] was already used to make the call, we won't make another call with the same params(obviously we have sorted the array beforehand to make sure duplicate elements occur together and we can recognize and skip duplicate calls)

PS: All the problem statements are taken from leetcode.com

THANK YOU FOR READING. All your suggestions are welcomed to improve more. Encourage me to write more by appreciating if you like the article.

SEE YOU IN THE NEXT PART WITH QUESTIONS RELATED TO COMBINATIONS :)

Software Developer working to become a better Software Developer everyday :)