This solution gives all the combinations of letters of a word.
For example if the input is ABC, the output will be:
A
B
C
AB
AC
BC
ABC
For example if the input is ABC, the output will be:
A
B
C
AB
AC
BC
ABC
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");
}
}