Find all possible combinations of String in java : code with example

Find all possible combinations of String in java : code with example

All combination of string in java  is the companion problem to find permutation of the string . The combination  generated from the algorithm has range in length from one to the  length of the string. Two combinations that differ only in ordering of their characters are the same combination. In other words, "12" and "31" are different combinations from the input string "123", but "21" is the same as "12".


Read Also :    Permutation of string in java with example


 Let us understand the string combination algorithm with example :

 If the input is "wxyz"  then the output of the string is

" w wx wxy wxyz wxz wy wyz wz x xy xyz xz y yz z "


Logic :

Start with an empty output string and the first character of the input as the input start position. For a given position , select all the letters sequentially from the input start position to the last letter in the input string. For each selected letter , append it to the output string , print the combination and then produce all other combinations starting with this sequence by recursively  calling the generating function with the input start position set to the next letter after the one we have just selected. Delete the character
we appended to the output after returning from the recursive call so that it create room for next character we select .


find all combinations of string in java algorithm


String Combinations algorithm Pseudo Code :

For each letter from input start position to end
    of input string

            *  Append the letter to the output string
            *  Print letters in output string
If the current letter isn’t the last in the input string
            * Generate remaining combinations
               starting at next position with
               iteration starting at next letter
               beyond  the letter just selected  
    
Delete the last character of the output string


Read Also :    Anagram Solver (Jumbled words to find meaningful word in Dictionary)
                       Find Permutation of String in Java using Anagram Solver      

Demo :

algorithm to find combination of string in java













Code :



 public class Combinations {
    private StringBuilder output = new StringBuilder();
    private final String inputstring;
    public Combinations( final String str ){
        inputstring = str;
        System.out.println("The input string  is  : " + inputstring);
    }
    
    
    public static void main (String args[])
    {
        Combinations combobj= new Combinations("wxyz");
        System.out.println("");
        System.out.println("");
        System.out.println("All possible combinations are :  ");
        System.out.println("");
        System.out.println("");
        combobj.combine();
    }
    
    public void combine() { combine( 0 ); }
    private void combine(int start ){
        for( int i = start; i < inputstring.length(); ++i ){
            output.append( inputstring.charAt(i) );
            System.out.println( output );
            if ( i < inputstring.length() )
            combine( i + 1);
            output.setLength( output.length() - 1 );
        }
    }
} 



Please write in comments in case you have any doubt.