• 1 day ago
Transcript
00:00First of all, I am going to create a function in the code. Public static. I will give it the
00:04name void find permutation.
00:09In the first one, I will have a string str and the second one will have a string answer.
00:16The answer, which is the variable, will increase and increase and increase. I will store my answer and permutation.
00:22And from the string, I will increase my permutation.
00:26First of all, I will do two work. One will be my base case and one will be my recursion.
00:31What do we need to do in the recursion? We need to take choices.
00:33Where do we need to take choices?
00:34Whatever the string is, we need to put one of letters in our current place.
00:40So, with int i is equal to 0, I will go to str.length.
00:46If I take a loop of current character, I will remove my current character.
00:51Character current is equal to str.char at i.
00:58So, the character on the i index is my current character.
01:01Now, what do I need to do? I need to add this current character in my answer.
01:06Before adding, I want to delete the current character in the string.
01:10So, how will I delete it?
01:11I will take a sub-string.
01:13So, here I am going to use the actual functions here.
01:16And I need to know these functions.
01:17I need to know these functions.
01:18The caret function, length function, sub-string function.
01:22These functions are important.
01:23And I need to know about these functions.
01:25If I have a string.
01:27For example, I have a string.
01:29A, B, C, D, E.
01:30If I have a string.
01:32If I have picked C, then what will I remove the function of C?
01:35What will I do?
01:36I will add A, B and D, E.
01:40What will happen?
01:41If I have taken a string and D, E.
01:44I will remove the function of C.
01:46Because I will give them both sum.
01:48What will I give them both?
01:48I will give them both sum.
01:50I will give them both sum.
01:51The same thing I will do.
01:53If I have picked the next character.
01:55How will the rest of the string come?
01:56For the rest of the string, I will take a sub-string.
01:59You have to take the next index from 0 to i.
02:04We have discussed in the sub-string method.
02:06This is the ending index.
02:07It is non-inclusive.
02:08It is exclusive.
02:10The next character.
02:12This is my current character.
02:15It will not come in the sub-string.
02:16It will come in the first of all.
02:18I have taken all of them.
02:20I have taken all of them.
02:21Plus str.sub-string.
02:23I will start with i plus 1.
02:25I will not come here.
02:26I will start with i minus 1.
02:28I will start with i plus 1.
02:30I will start with i plus 1.
02:31What can I say?
02:31That i will completely corrupt.
02:33Like I have copied c.
02:35I have copied the character here.
02:37This will start with i plus 1.
02:39I can write string.length minus 1.
02:41No.
02:42I have learned the sub-string.
02:43There are two variations.
02:44If I don't write anything in the last.
02:45That means n minus 1.
02:46I will start with i plus 1.
02:50Then I have done this.
02:50Then I have removed string.
02:51Then I have removed string.
02:54Then I have removed string.
02:56Then I have removed string.
02:57Then I will call to find permutation.
02:59Then I will pass string.
03:02Then I will add my answer.
03:03I will add my current character with answer.
03:06So by this way.
03:08The current character will be appended.
03:11and this will be my call.
03:14What will be in the base case?
03:15In the base case, the length of my string is equal to 0.
03:19The string is almost empty.
03:23Now I will print my answer and I will return.
03:27With the main function, I have created a string.
03:29I will print and find all permutations.
03:34I will pass a empty string as my answer.
03:37I will save and run.
03:42A, B, C and C also have been printed.
03:45Why did I print that?
03:46Why did I call the rest?
03:47This happened because I have changed here.
03:51What happened here?
03:53The length of string is calculated.
03:55This variable is n.
03:57We have done n-1.
03:59Then I have done n-1.
04:01Then I have done n-1.
04:02Then I have done n-2.
04:03Then I have done n-2.
04:05I have done n-1.
04:06Then I have done n-1.
04:10I have done n-1.
04:11What will I pass the next level?
04:12I will pass the new string to the next level.
04:16This will be my new string.
04:19What will I do?
04:21I have removed the next character.
04:23Then I have made a new string.
04:25Because we will change the same string,
04:26then we will change the same string.
04:27Then this loop will not go away.
04:29I will save and run again.
04:32Then I will save and run again.
04:33Then I have all my permutations.
04:35I have 6 permutations that I have calculated.
04:38And this is equal to my n factorial.
04:40That means I have calculated 3 factorial permutations.
04:42I have calculated.
04:43I have calculated.
04:44Now in a permutation,
04:46I know that I have n time.
04:48So in n factorial permutations,
04:50I have n into n factorial time.
04:53That is why the time complexity of this code
04:55is equal to big of n into n factorial.