Interview Question

Software Engineer Interview

Print all permutation of a given string.

Answer

Interview Answer

1 Answer

0

public static void allPermutations(String s)
{
    allP(s, "");
}

public static void allP(String s, String soFar)
{
    if (s.equals("")) System.out.println(soFar);
    for (int i = 0; i < s.length(); i++)
    {
        allP(s.substring(0,i) + s.substring(i+1,s.length()),soFar + s.substring(i,i+1));
    }
}

Rahul on May 3, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.