Print out all possible letter combinations a given phone number can represent
I am working on a problem in which given a 7 digit telephone number, we need to print out all possible combinations of letters that each number could represent.
I came up with below code and I was wondering if there is any way to optimize it or if there is any better way? As of now complexity is O(n3).
public class LetterCombinationsOfPhoneNumber {
private static final HashMap<Character, String> map = new HashMap<>(8);
static {
map.put('2', "abc");
map.put('3', "def");
map.put('4', "ghi");
map.put('5', "jkl");
map.put('6', "mno");
map.put('7', "pqrs");
map.put('8', "tuv");
map.put('9', "wxyz");
};
public static List<String> letterCombinations(String digits) {
LinkedList<String> results = new LinkedList<>();
results.add("");
for (int i = 0; i < digits.length(); i++) {
String letters = map.get(digits.charAt(i));
for (int j = results.size(); j > 0; j--) {
String intermediateResult = results.poll();
for (int k = 0; k < letters.length(); k++) {
results.add(intermediateResult + letters.charAt(k));
}
}
}
return results;
}
public static void main(String args) {
System.out.println(letterCombinations("23"));
}
}
java algorithm combinatorics
add a comment |
I am working on a problem in which given a 7 digit telephone number, we need to print out all possible combinations of letters that each number could represent.
I came up with below code and I was wondering if there is any way to optimize it or if there is any better way? As of now complexity is O(n3).
public class LetterCombinationsOfPhoneNumber {
private static final HashMap<Character, String> map = new HashMap<>(8);
static {
map.put('2', "abc");
map.put('3', "def");
map.put('4', "ghi");
map.put('5', "jkl");
map.put('6', "mno");
map.put('7', "pqrs");
map.put('8', "tuv");
map.put('9', "wxyz");
};
public static List<String> letterCombinations(String digits) {
LinkedList<String> results = new LinkedList<>();
results.add("");
for (int i = 0; i < digits.length(); i++) {
String letters = map.get(digits.charAt(i));
for (int j = results.size(); j > 0; j--) {
String intermediateResult = results.poll();
for (int k = 0; k < letters.length(); k++) {
results.add(intermediateResult + letters.charAt(k));
}
}
}
return results;
}
public static void main(String args) {
System.out.println(letterCombinations("23"));
}
}
java algorithm combinatorics
The number of outputs will be 3^n or more (not n^3), so that's obviously a lower bound on the complexity. You won't be able to get lower than that without omitting some of the results.
– Toby Speight
Oct 4 '18 at 7:43
One small hint regarding thenew 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.
– Ralf Kleberhoff
Oct 4 '18 at 20:56
add a comment |
I am working on a problem in which given a 7 digit telephone number, we need to print out all possible combinations of letters that each number could represent.
I came up with below code and I was wondering if there is any way to optimize it or if there is any better way? As of now complexity is O(n3).
public class LetterCombinationsOfPhoneNumber {
private static final HashMap<Character, String> map = new HashMap<>(8);
static {
map.put('2', "abc");
map.put('3', "def");
map.put('4', "ghi");
map.put('5', "jkl");
map.put('6', "mno");
map.put('7', "pqrs");
map.put('8', "tuv");
map.put('9', "wxyz");
};
public static List<String> letterCombinations(String digits) {
LinkedList<String> results = new LinkedList<>();
results.add("");
for (int i = 0; i < digits.length(); i++) {
String letters = map.get(digits.charAt(i));
for (int j = results.size(); j > 0; j--) {
String intermediateResult = results.poll();
for (int k = 0; k < letters.length(); k++) {
results.add(intermediateResult + letters.charAt(k));
}
}
}
return results;
}
public static void main(String args) {
System.out.println(letterCombinations("23"));
}
}
java algorithm combinatorics
I am working on a problem in which given a 7 digit telephone number, we need to print out all possible combinations of letters that each number could represent.
I came up with below code and I was wondering if there is any way to optimize it or if there is any better way? As of now complexity is O(n3).
public class LetterCombinationsOfPhoneNumber {
private static final HashMap<Character, String> map = new HashMap<>(8);
static {
map.put('2', "abc");
map.put('3', "def");
map.put('4', "ghi");
map.put('5', "jkl");
map.put('6', "mno");
map.put('7', "pqrs");
map.put('8', "tuv");
map.put('9', "wxyz");
};
public static List<String> letterCombinations(String digits) {
LinkedList<String> results = new LinkedList<>();
results.add("");
for (int i = 0; i < digits.length(); i++) {
String letters = map.get(digits.charAt(i));
for (int j = results.size(); j > 0; j--) {
String intermediateResult = results.poll();
for (int k = 0; k < letters.length(); k++) {
results.add(intermediateResult + letters.charAt(k));
}
}
}
return results;
}
public static void main(String args) {
System.out.println(letterCombinations("23"));
}
}
java algorithm combinatorics
java algorithm combinatorics
edited Oct 4 '18 at 6:30
200_success
129k15152414
129k15152414
asked Oct 4 '18 at 4:38
dragonsdragons
111
111
The number of outputs will be 3^n or more (not n^3), so that's obviously a lower bound on the complexity. You won't be able to get lower than that without omitting some of the results.
– Toby Speight
Oct 4 '18 at 7:43
One small hint regarding thenew 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.
– Ralf Kleberhoff
Oct 4 '18 at 20:56
add a comment |
The number of outputs will be 3^n or more (not n^3), so that's obviously a lower bound on the complexity. You won't be able to get lower than that without omitting some of the results.
– Toby Speight
Oct 4 '18 at 7:43
One small hint regarding thenew 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.
– Ralf Kleberhoff
Oct 4 '18 at 20:56
The number of outputs will be 3^n or more (not n^3), so that's obviously a lower bound on the complexity. You won't be able to get lower than that without omitting some of the results.
– Toby Speight
Oct 4 '18 at 7:43
The number of outputs will be 3^n or more (not n^3), so that's obviously a lower bound on the complexity. You won't be able to get lower than that without omitting some of the results.
– Toby Speight
Oct 4 '18 at 7:43
One small hint regarding the
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 a new 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.– Ralf Kleberhoff
Oct 4 '18 at 20:56
One small hint regarding the
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 a new 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.– Ralf Kleberhoff
Oct 4 '18 at 20:56
add a comment |
2 Answers
2
active
oldest
votes
This can certainly be optimized.
Your algorithm works in loops that iterate over all permutations, placing one letter each time.
Let us take a different approach:
The number of output combinations can be calculated in advanced: multiply the number of letters for all input numbers. in yout example ("23"
) the calculation is
number 2 has 3 letters
number 3 has 3 letters
output combinations will be 3*3=9
Furthermore, we can calculate which letter will be placed in each combination based on the combination index. if we assume combination index goes from 0 to 8:
for number 2 (first number in input)
- combinations 0-2 will contain 1st letter
- combinations 3-5 will contain 2nd letter
- combinations 6-8 will contain 3rd letter
for number 3 (2nd number in input)
- combinations 0,3,6 will contain 1st letter
- combinations 1,4,7 will contain 2nd letter
- combinations 2,5,8 will contain 3rd letter
so the formula for letter placement is based on the combination index, the position of the number in the input and the position of the letter in the letters-of-number String.
the complexity of this algorithm is o(n*m) where n is number of input letters and m number of output combinations.
one comment regarding the code: You use the results
as a Queue
. the LinkedList
is an implementation of this interface. For clarity sake, the variable should be defined with the type that states its usage. This is also true for the map
variable (should be defined as Map
)
good suggestion. can you provide an example in java so that I can understand better on how would this work? Also complexity will be same as my current solution?
– dragons
Oct 4 '18 at 15:18
add a comment |
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 contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f204910%2fprint-out-all-possible-letter-combinations-a-given-phone-number-can-represent%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can certainly be optimized.
Your algorithm works in loops that iterate over all permutations, placing one letter each time.
Let us take a different approach:
The number of output combinations can be calculated in advanced: multiply the number of letters for all input numbers. in yout example ("23"
) the calculation is
number 2 has 3 letters
number 3 has 3 letters
output combinations will be 3*3=9
Furthermore, we can calculate which letter will be placed in each combination based on the combination index. if we assume combination index goes from 0 to 8:
for number 2 (first number in input)
- combinations 0-2 will contain 1st letter
- combinations 3-5 will contain 2nd letter
- combinations 6-8 will contain 3rd letter
for number 3 (2nd number in input)
- combinations 0,3,6 will contain 1st letter
- combinations 1,4,7 will contain 2nd letter
- combinations 2,5,8 will contain 3rd letter
so the formula for letter placement is based on the combination index, the position of the number in the input and the position of the letter in the letters-of-number String.
the complexity of this algorithm is o(n*m) where n is number of input letters and m number of output combinations.
one comment regarding the code: You use the results
as a Queue
. the LinkedList
is an implementation of this interface. For clarity sake, the variable should be defined with the type that states its usage. This is also true for the map
variable (should be defined as Map
)
good suggestion. can you provide an example in java so that I can understand better on how would this work? Also complexity will be same as my current solution?
– dragons
Oct 4 '18 at 15:18
add a comment |
This can certainly be optimized.
Your algorithm works in loops that iterate over all permutations, placing one letter each time.
Let us take a different approach:
The number of output combinations can be calculated in advanced: multiply the number of letters for all input numbers. in yout example ("23"
) the calculation is
number 2 has 3 letters
number 3 has 3 letters
output combinations will be 3*3=9
Furthermore, we can calculate which letter will be placed in each combination based on the combination index. if we assume combination index goes from 0 to 8:
for number 2 (first number in input)
- combinations 0-2 will contain 1st letter
- combinations 3-5 will contain 2nd letter
- combinations 6-8 will contain 3rd letter
for number 3 (2nd number in input)
- combinations 0,3,6 will contain 1st letter
- combinations 1,4,7 will contain 2nd letter
- combinations 2,5,8 will contain 3rd letter
so the formula for letter placement is based on the combination index, the position of the number in the input and the position of the letter in the letters-of-number String.
the complexity of this algorithm is o(n*m) where n is number of input letters and m number of output combinations.
one comment regarding the code: You use the results
as a Queue
. the LinkedList
is an implementation of this interface. For clarity sake, the variable should be defined with the type that states its usage. This is also true for the map
variable (should be defined as Map
)
good suggestion. can you provide an example in java so that I can understand better on how would this work? Also complexity will be same as my current solution?
– dragons
Oct 4 '18 at 15:18
add a comment |
This can certainly be optimized.
Your algorithm works in loops that iterate over all permutations, placing one letter each time.
Let us take a different approach:
The number of output combinations can be calculated in advanced: multiply the number of letters for all input numbers. in yout example ("23"
) the calculation is
number 2 has 3 letters
number 3 has 3 letters
output combinations will be 3*3=9
Furthermore, we can calculate which letter will be placed in each combination based on the combination index. if we assume combination index goes from 0 to 8:
for number 2 (first number in input)
- combinations 0-2 will contain 1st letter
- combinations 3-5 will contain 2nd letter
- combinations 6-8 will contain 3rd letter
for number 3 (2nd number in input)
- combinations 0,3,6 will contain 1st letter
- combinations 1,4,7 will contain 2nd letter
- combinations 2,5,8 will contain 3rd letter
so the formula for letter placement is based on the combination index, the position of the number in the input and the position of the letter in the letters-of-number String.
the complexity of this algorithm is o(n*m) where n is number of input letters and m number of output combinations.
one comment regarding the code: You use the results
as a Queue
. the LinkedList
is an implementation of this interface. For clarity sake, the variable should be defined with the type that states its usage. This is also true for the map
variable (should be defined as Map
)
This can certainly be optimized.
Your algorithm works in loops that iterate over all permutations, placing one letter each time.
Let us take a different approach:
The number of output combinations can be calculated in advanced: multiply the number of letters for all input numbers. in yout example ("23"
) the calculation is
number 2 has 3 letters
number 3 has 3 letters
output combinations will be 3*3=9
Furthermore, we can calculate which letter will be placed in each combination based on the combination index. if we assume combination index goes from 0 to 8:
for number 2 (first number in input)
- combinations 0-2 will contain 1st letter
- combinations 3-5 will contain 2nd letter
- combinations 6-8 will contain 3rd letter
for number 3 (2nd number in input)
- combinations 0,3,6 will contain 1st letter
- combinations 1,4,7 will contain 2nd letter
- combinations 2,5,8 will contain 3rd letter
so the formula for letter placement is based on the combination index, the position of the number in the input and the position of the letter in the letters-of-number String.
the complexity of this algorithm is o(n*m) where n is number of input letters and m number of output combinations.
one comment regarding the code: You use the results
as a Queue
. the LinkedList
is an implementation of this interface. For clarity sake, the variable should be defined with the type that states its usage. This is also true for the map
variable (should be defined as Map
)
answered Oct 4 '18 at 7:26
Sharon Ben AsherSharon Ben Asher
2,226512
2,226512
good suggestion. can you provide an example in java so that I can understand better on how would this work? Also complexity will be same as my current solution?
– dragons
Oct 4 '18 at 15:18
add a comment |
good suggestion. can you provide an example in java so that I can understand better on how would this work? Also complexity will be same as my current solution?
– dragons
Oct 4 '18 at 15:18
good suggestion. can you provide an example in java so that I can understand better on how would this work? Also complexity will be same as my current solution?
– dragons
Oct 4 '18 at 15:18
good suggestion. can you provide an example in java so that I can understand better on how would this work? Also complexity will be same as my current solution?
– dragons
Oct 4 '18 at 15:18
add a comment |
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 contributor
add a comment |
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 contributor
add a comment |
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 contributor
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 contributor
New contributor
answered 25 mins ago
AweuzegagaAweuzegaga
11
11
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f204910%2fprint-out-all-possible-letter-combinations-a-given-phone-number-can-represent%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The number of outputs will be 3^n or more (not n^3), so that's obviously a lower bound on the complexity. You won't be able to get lower than that without omitting some of the results.
– Toby Speight
Oct 4 '18 at 7:43
One small hint regarding the
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.– Ralf Kleberhoff
Oct 4 '18 at 20:56