A
Anakin
I have this simple recursion problem that i need to solve. It starts
with the following string:
String element = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
I need to generate and print on screen all combinations of uppercase
letters, given a group of starting letters. In other words, if I say
that I want all combinations of the first three letters of the alphabet
– ABC – then I need to generate ABC, AB, AC, BC, A, B, C. The order of
the letters is not actually important. So I need to write a recursive
implementation of a method that will generate all such strings for a
given number of letters. In other words, the user would give me the
letter count and I provide the list of combinations. (for testing
purposes, I could print this on screen). Also, I'm not allowed to use
any loops for my recursion.
I tried to do this recursion part, but this is driving me crazy. I only
found how to do this using loops. There must be an easier way without loops.
please help. thanks.
Paul M.
public static void recursive(String S, Set dataSet){
int len = S.length();
String temp = "";
if (len == 1) {
System.out.println(S);
}
else {
System.out.println(S);
for (int j=len-1; j>=0; j--) {
for (int k=0; k<len; k++) {
if (k != j) temp += S.charAt(k);
}
System.out.println(S);
recursive(temp, dataSet);
temp = "";
}
}
}
with the following string:
String element = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
I need to generate and print on screen all combinations of uppercase
letters, given a group of starting letters. In other words, if I say
that I want all combinations of the first three letters of the alphabet
– ABC – then I need to generate ABC, AB, AC, BC, A, B, C. The order of
the letters is not actually important. So I need to write a recursive
implementation of a method that will generate all such strings for a
given number of letters. In other words, the user would give me the
letter count and I provide the list of combinations. (for testing
purposes, I could print this on screen). Also, I'm not allowed to use
any loops for my recursion.
I tried to do this recursion part, but this is driving me crazy. I only
found how to do this using loops. There must be an easier way without loops.
please help. thanks.
Paul M.
public static void recursive(String S, Set dataSet){
int len = S.length();
String temp = "";
if (len == 1) {
System.out.println(S);
}
else {
System.out.println(S);
for (int j=len-1; j>=0; j--) {
for (int k=0; k<len; k++) {
if (k != j) temp += S.charAt(k);
}
System.out.println(S);
recursive(temp, dataSet);
temp = "";
}
}
}