1

Possible Duplicate:
Generating all permutations of a given string

I´m looking for a algorithm which returns me a List of all possible combines of x-letters.

Example: 3 letters. (A,B,C)

  • A B C
  • A C B
  • B C A
  • B A C
  • C B A
  • C A B

I want to do this up to 4-5 letters with a algorithm in Java.

Community
  • 1
  • 1
user959456
  • 565
  • 1
  • 9
  • 13

2 Answers2

1

What you are looking for is all permutations of a string.

Here is an example from Princeton cs done in Java.

onit
  • 6,306
  • 3
  • 24
  • 31
0
void printAllLetterSequences(String prefix, int length) {
    System.out.println(prefix);
    if (prefix.size() < length)
        for (char c = 'A'; c <= 'D'; c++)
            printAllLetterSequences(prefix + c, length);
}
bezmax
  • 25,562
  • 10
  • 53
  • 84
Zain Khan
  • 3,753
  • 3
  • 31
  • 54
  • thank you for your answer. your algorithm returns a list of characters which contains combines with 3 characters when i need 4. And I only want to get each character ones in the combine. For example "ABBA" not. – user959456 Apr 02 '12 at 14:25
  • @user959456 Better check the duplicate question mentioned in comments (http://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string). It's more universal as it works with any kind of character sequences. – bezmax Apr 02 '12 at 14:26