Ouput all character combinations of string

Status
Not open for further replies.
Level 22
Joined
Feb 3, 2009
Messages
3,292
Hello, I've struggled on google for a while and wasn't able to find the exact thing I need, only a close approximation which is insufficient.

So I'd like to ask here, for a conceptual way to write all combinations from an input string, example:

Input: ABC (Needs to work for any length)

Output:

A
B
C
AA
AB
AC
BA
BB
BC
CA
CB
CC
AAA
AAB
AAC
ABA
ABB
ABC
ACA
ACB
ACC
BAA
BAB
BAC
BBA
BBB
BBC
BCA
BCB
BCC
CAA
CAB
CAC
CBA
CBB
CBC
CCA
CCB
CCC

Bellow is the code of the program which does a similar thing, but not fully what I need:

Code:
public class StringPermutation {    
    
    String input;   //input String  
    StringBuffer output;  //Holds the output  
      
    /* 
     * Generates combinations by applying the  
     * concept of recursion 
     */  
    private void nextCombination(int position, int remainder)   
    {       
        output.append(input.charAt(position));    
          
        if (remainder == 1) {         
          System.out.println(output);       
        } else {         
          for (int i = position + 1; i + remainder - 1 <= input.length(); i++)           
            nextCombination(i, remainder - 1);       
        }       
          
        output.deleteCharAt(output.length() - 1);   
    }      
      
    public void generateCombinations(String input)   
    {       
          output = new StringBuffer();       
          this.input = input;  
          int inputStrLength = input.length();  
            
          for (int i = 1; i <= inputStrLength; i++)         
              for (int j = 0; j + i <= inputStrLength; j++)           
                  nextCombination(j, i);     
    }   
    
    /**  
     * @param args  
     */    
    public static void main(String[] args) {             
        new StringPermutation().generateCombinations("ABC");    
    
    }    
}

Which outputs:

A
B
C
AB
AC
BC
ABC
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Get the length of the input - this is your depth.
Loop over every character in the input, with the length as an argument.
Append every character in the input.

So far so good.

Now for every character restart the process, while reducing the depth.
Append the character to the input string.
Append the string to the permutation array.

When depth == 1, quit.

And that text is written really badly, so I'll give an example - for ABC, the order of operations and depths will look like this:
Code:
A 3
    AA 2
        AAA 1
        AAB 1
        AAC 1
    AB 2
        ABA 1
        ABB 1
        ABC 1
    AC 2
        ACA 1
        ACB 1
        ACC 1
B 3
    BA 2
        BAA 1
        BAB 1
        BAC 1
    BB 2
        BBA 1
        BBB 1
        BBC 1
    BC 2
        BCA 1
        BCB 1
        BCC 1
C 3
    CA 2
        CAA 1
        CAB 1
        CAC 1
    CB 2
        CBA 1
        CBB 1
        CBC 1
    CC 2
        CCA 1
        CCB 1 
        CCC 1

JavaScript:
function _stringPermutations(current, input, buffer, depth) {
    if (depth > 1) {
        for (var i = 0; i < input.length; i++) {
            buffer.push(current + input[i]);
            
            _stringPermutations(current + input[i], input, buffer, depth - 1);
        }
    }
}

function stringPermutations(input) {
    var buffer = [];
    
    for (var i = 0; i < input.length; i++) {
        buffer.push(input[i]);
        
        _stringPermutations(input[i], input, buffer, input.length);
    }
    
    return buffer;
}
 
Status
Not open for further replies.
Top