I am trying to understand the code written below. I can follow it up to a point. I think I understand how the first result is achieved but unsure of how the rest are. Below is the code and my interpretation.
Code:
public static void main(String [] args)
{
String str ="abc";
permuatation(str,"");
}
private static void permuatation(String str, String current) {
if(str.equals(""))
{
System.out.printf("Result: %s",current);
System.out.println();
}
else
{
for (int i = 0; i < str.length(); i++)
{
char c= str.charAt(i);
permuatation(str.substring(0, i)+ str.substring(i+1), current + c);
}
}
My interpretation for a string such as abc;
1.for i = 0; i < than string length; increment i
2.at index i convert to character(a);
3.run permutation method using the argument of substring(remaining characters -bc) and(current character a);
4.Now using string bc
5.for i = 0; i < than string length; increment i
6.at index i convert to character(b);
7.run permutation method using the argument of substring(remaining characters -c) and(current character ab);
8.for i = 0; i < than string length; increment i
9.at index i convert to character(b);
10.run permutation method using the argument of substring(remaining characters -) and(current character abc);
11 as string = "" print current characters abc
How are the other permutations achieved? Any guidance would be much appreciated.