Combining the comments saying that 163
can be 1,6,3
or 16,3
, but not 1,63
, and user3437460's suggestion of using recursion:
- Take first digit and convert to letter. Make recursive call using letter and remaining digits.
- Take first two digits. If
<=26
, convert to letter and make recursive call using letter and remaining digits.
Here is the code. Since I have no clue what to call the method, I'm going with x
.
public static void main(String[] args) {
x("11112", "");
System.out.println("------");
x("163", "");
}
private static final void x(String digits, String word) {
if (digits.isEmpty())
System.out.println(word);
else {
int num = Integer.parseInt(digits.substring(0, 1));
x(digits.substring(1), word + (char)('A' + num - 1));
if (digits.length() >= 2 && (num = Integer.parseInt(digits.substring(0, 2))) <= 26)
x(digits.substring(2), word + (char)('A' + num - 1));
}
}
Output
AAAAB
AAAL
AAKB
AKAB
AKL
KAAB
KAL
KKB
------
AFC
PC