Wednesday, May 9, 2012

Program to print all combinations of a string


import java.io.*;
public class combinations
{
    private static void printPermsAux(String s,String so)
    {
        if(s.equals(""))
        System.out.println(so);
        for (int i=0;i<s.length();++i)
        {
            printPermsAux(s.substring(0,i)+s.substring(i+1),so+s.charAt(i));
        }
    }
    private static void printPerms(String n)
    {
        printPermsAux(n,"");
    }
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter The Characters:");
        String input=br.readLine();
        System.out.println("Possible Combinations......");
        printPerms(input);
    }
}
//-------------------------------------------------------------------------
/**
 Output:-

 Enter The Characters:ABC
 Possible Combinations......
 ABC
 ACB
 BAC
 BCA
 CAB
 CBA

 Enter The Characters:AZBY
 Possible Combinations......
 AZBY
 AZYB
 ABZY
 ABYZ
 AYZB
 AYBZ
 ZABY
 ZAYB
 ZBAY
 ZBYA
 ZYAB
 ZYBA
 BAZY
 BAYZ
 BZAY
 BZYA
 BYAZ
 BYZA
 YAZB
 YABZ
 YZAB
 YZBA
 YBAZ
 YBZA
 */

0 comments:

Post a Comment