Amazon.com

  www.amazon.com
  www.amazon.com

Interview Question

Software Engineer Intern Interview Seattle, WA

Given a string, output all its permutations

Answer

Interview Answer

1 Answer

0

public class StringPerm {

    public static void Permutation(String strsub, String str)
    {
        int length=str.length();
        //System.out.println(length);
        if(length<=1)
            System.out.println(strsub+str);
        else
        {
            for(int i=0;i<length;i++)
            {
                String newString=str.substring(0, i)+str.substring(i+1);
                Permutation(strsub+str.charAt(i),newString);
            }
        }

    }
    public static void main(String []args)
    {
        String str,strsub;
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string to be permuted : ");
        str=in.nextLine();
        strsub="";
        System.out.println("Possible Permutations of Strings : ");
        Permutation(strsub,str);

    }
}

Mahesh Babu on Feb 9, 2013

Add Answers or Comments

To comment on this Question, Sign In with Facebook or Sign Up