Let's now get into the code for solving the permutations question.
So we have discussed the method that we are going to use in detail in the previous video.
And as we have discussed, we will need a results array where we will be storing all the permutations
as we get to a permutation.
And finally we will be returning this okay.
And this is going to be a recursive solution.
So we are going to need a helper function.
Let me just call it helper itself.
And to this function we will be passing I or an index okay.
And let's write the details later.
But over here when we call this helper function for the first time, we will be passing the index zero
okay, which is the first index of the numb's array which is given to us.
Now let's go ahead and write the details of this helper function.
So we will be both writing the base case as well as the recursive case.
Now the base case would be the scenario where I, which is the index that is passed to the helper function,
is equal to the last index of the Nums array.
Because if that is the case, as we have discussed in the previous video, there is no value beyond
it.
So there is no point swapping the element at that particular index with anything else because there's
nothing beyond it.
So if I is equal to the last index, which is equal to n minus one, if n is the length of the numb's
array.
So let's write that over here.
So let's say let n is equal to numb's dot length.
So if this is the case and if I is equal to n minus one we would have hit the base case.
So there is no more swapping to be done.
And all that we need to do is we just need to make a copy of the current state of the Nums array and
push it to us.
Okay, so I can say rest or push.
And over here we are going to make a copy of the Nums array at this particular state.
And again, the reason why we need to do this is because this is a backtracking solution.
So as we have discussed, we will be making changes to the state of the problem in place.
So at a later stage, the way that the numb's array looks would be different.
So that's why we are making a snapshot.
Or we are making a copy of how the Numb's array looks at this particular point, and we are pushing
it to the results array.
And then we can just return.
Okay.
So we are stopping going further down that particular branch of the recursive tree.
And we are returning.
Now if this is not the case then we proceed to the recursive case.
Now this is the case where I is not equal to n minus one.
And in this case we will need another variable.
Let's call it j.
And j will take the values starting at I, and it will go up to the last valid index which is n minus
one okay.
And for each value of j we are going to do three things.
First, we are going to swap the elements at index I and index j.
So I can say numb's at I comma numb's j is equal to numb's at j comma numb's at I.
Okay, so I have swapped these two elements and then we will go deeper down that particular branch of
the recursive tree.
So for that I will recursively call the helper function.
And we will be passing I plus one to it.
Now after we come back over here, that is after we have gone down all the way till the base case in
that particular branch on the recursive tree, and we get back over here.
Then we'll have to proceed and do the backtracking step.
Okay.
And remember this is important because we are making changes to the state of the problem in place,
or we are changing numb's the Nums array itself to get the various permutations, and then to explore
another branch in the recursive tree, we need to keep reverting the changes we have made.
So that is what we are doing over here.
So I can say numb's I comma numb's j is equal to numb's j comma Numb's I.
Okay, so again we are reverting the changes that we made in this line of code over here.
And this is the backtracking step.
All right.
So that's it.
So this should solve the problem.
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 in case you're stuck and need help.
Do make use of the user logs which are provided over here for every test case.