You do it by actually using the i
parameter.
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static void binary(int n, String str, int i) {
if (i == ALPHABET.length())
return;
if (n == 0) {
System.out.println(str);
return;
}
binary(n - 1, str + ALPHABET.charAt(i), 0); // next letter starts at beginning of alphabet
binary(n, str, i + 1); // do it again using next letter of alphabet
}
TEST
binary(2, "", 0);
OUTPUT
aa
ab
ac
...
az
ba
bb
...
zy
zz
0
solved How can I convert my for loop into a recursive algorithm?