0

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.

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
Calgar99
  • 1,670
  • 5
  • 26
  • 42
  • 2
    I suggest that you use a debugger and step through the code one line at a time and watch how the variable values change. If you find something more specific that you don't understand, please come back with more questions! – Code-Apprentice Mar 25 '13 at 23:53
  • Searching does not hurt. No, really! http://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string – drzymala Mar 26 '13 at 00:35
  • It is permutation via recursion. Better explained here: http://stackoverflow.com/questions/5438960/permutations-recursion?answertab=votes#tab-top – aartist Mar 26 '13 at 01:12

0 Answers0