IMO there is a better way.
Firstly you store every combination in a List. You are doing more than what is asked by giving the possibility to use an arbitrary phone numbers length but you will eventually run out of memory past a certain length.
Secondly you are able to know beforehand the numbers of possibilities you will have so you do not need to go for a double or triple for
loop.
So how to do it in one loop in java :
public class LetterCombination {
// Mappings from 0 to 9.
// With 0 and 1 with no mappings because none is given in our instructions
public static String mappings[][] = {
{""}, {""}, {"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"},
{"J", "K", "L"}, {"M", "N", "O"}, {"P", "Q", "R", "S"},
{"T", "U", "V"}, {"W", "X", "Y", "Z"}
};
public static void letterCombinations(String digits) {
// The exercise specify that we will receive a phone number of 7 digits.
// We suppose that the validation of the String received is done before.
// All our digits are converted to int.
int firstDigit = Integer.parseInt(digits.substring(0,1));
int secondDigit = Integer.parseInt(digits.substring(1,2));
int thirdDigit = Integer.parseInt(digits.substring(2,3));
int fourthDigit = Integer.parseInt(digits.substring(3,4));
int fifthDigit = Integer.parseInt(digits.substring(4,5));
int sixthDigit = Integer.parseInt(digits.substring(5,6));
int seventhDigit = Integer.parseInt(digits.substring(6,7));
// To each digits is associated its number of possibilities
// (From 3 to 4 in our exercise)
int firstDigitPossibilities = mappings[firstDigit].length;
int secondDigitPossibilities = mappings[secondDigit].length;
int thirdDigitPossibilities = mappings[thirdDigit].length;
int fourthDigitPossibilities = mappings[fourthDigit].length;
int fifthDigitPossibilities = mappings[fifthDigit].length;
int sixthDigitPossibilities = mappings[sixthDigit].length;
int seventhDigitPossibilities = mappings[seventhDigit].length;
// We will have between 3^7 and 4^7 iterations
// We can have our number of iterations by multiplying each possibilities
for(int i = 0; i < firstDigitPossibilities * secondDigitPossibilities * thirdDigitPossibilities * fourthDigitPossibilities * fifthDigitPossibilities * sixthDigitPossibilities * seventhDigitPossibilities ; i++) {
// What is left is to print everything.
// Last number is printed like this :
// * mappings[last Digit][i modulo last Digit possibilities]
// Next Number is printed like this :
// * mapping [next Digit][( i / last Digit possibilities) modulo next Digit possibilities]
// And so on...
System.out.println(
mappings[firstDigit][(i/(secondDigitPossibilities*thirdDigitPossibilities*fourthDigitPossibilities*fifthDigitPossibilities*sixthDigitPossibilities*seventhDigitPossibilities))%firstDigitPossibilities]
+ mappings[secondDigit][(i/thirdDigitPossibilities*fourthDigitPossibilities*fifthDigitPossibilities*sixthDigitPossibilities*seventhDigitPossibilities)%secondDigitPossibilities]
+ mappings[thirdDigit][(i/(fourthDigitPossibilities*fifthDigitPossibilities*sixthDigitPossibilities*seventhDigitPossibilities))%thirdDigitPossibilities]
+ mappings[fourthDigit][(i/(fifthDigitPossibilities*sixthDigitPossibilities*seventhDigitPossibilities))%fourthDigitPossibilities]
+ mappings[fifthDigit][(i/(sixthDigitPossibilities*seventhDigitPossibilities))%fifthDigitPossibilities]
+ mappings[sixthDigit][(i/(seventhDigitPossibilities))%sixthDigitPossibilities]
+ mappings[seventhDigit][i%seventhDigitPossibilities]);
}
}
public static void main(String[] args) {
letterCombinations("23456789");
}
}
new HashMap<>(8)
: in your case, it's not worth deciding on an initial capacity. And if you really care, you should start with a bigger capacity, as anew HashMap<>(8)
will not accept 8 entries without growth, but only 6. The default load factor is 75%, meaning that the HashMap will grow as soon as it's 75% filled. \$\endgroup\$