Let's now get into the code for solving permutations two.
Now we have already written the code for permutations one previously.
And over here that's just going to be a slight tweak.
So let's get into it.
So we will need a variable.
Let's just call it rest which would be the results array okay.
And finally we will be returning rest.
All right.
And this is going to be a recursive solution.
So we will need a helper function.
Let me just call it permutations.
And to this function we will be passing a particular index.
Now this is pretty similar to what we have done for permutations one okay.
And we also have to call this function for the first time.
And at this point we will be passing the index zero to it.
So no change so far.
Now inside the permutations function we will have to write the base case as well as the recursive case.
Now the base case is if index is the last valid index, then we'll just make a copy of the particular
instance of numb's and we will push it to rest.
And that was the same base case which we had written for permutations one as well.
So no change over there as well.
So if index is equal to numb's dot length minus one, then we'll just make a copy of Numb's and push
it to rest.
All right.
And we can return.
So we are basically returning back from that particular branch on the recursive tree.
Now if this is not the case then again we would need another variable.
So I can say for where j is equal to index and j will take the values from index up to the last valid
index in the numb's array.
Okay.
Now, what is it that we had previously written over here?
We would swap the values at index I and j.
So we had written numb's I comma Numb's j is equal to numb's j comma Numb's I.
And we had made a recursive call to the helper function.
So permutations index plus one.
And again we had the backtracking step which is to revert the change that we just now did.
And again over here we don't have I we have used index.
So we have to write index over here and over here.
And we have to revert the change.
So again we can say numb's index comma Numb's j is equal to numb's j comma Numb's index.
Okay.
So this is what we had written for permutations one.
But the change that we have to do over here, as we have discussed in the previous video, is that for
a particular value of index, we will not take the case where numb's at j is repeating.
Okay.
So for example, at this particular index, like let us say index has a particular value.
And let's say J is getting a particular value for a particular index.
And if that value is repeating then we will prune that branch.
So that's what we have discussed in detail in the previous video.
And to implement this let's use a hash table.
And before we go ahead and do these operations we will check whether numb's and J is in the hash table
or not.
Okay.
So over here let me do that.
So if not hash numb's at J then we proceed with these steps which we have over here.
And if Numb's AJ is not in the hash table, we'll also have to add it to the hash table over here.
So I can say hash numb's j is equal to one.
Okay.
So we're just adding it to the hash table so that next time like let's say j takes another value.
And again numb's at j at that point is equal to the value numb's at j has at this point.
Then that particular value will already be there in the hash table, and therefore these lines of code
will not be executed.
And in this way we are pruning that particular branch in the recursive tree.
So that's it.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's passing all the test cases.
Also, remember to make use of the user logs which are provided for each test case in case you're stuck
at some place and you want to debug your code.