Skip to content

Commit 51fcc66

Browse files
samuelfacSamuel Facchinellovil02
authored
refactor: redesign LetterCombinationsOfPhoneNumber (#5221)
* Refactor * fix clang * fix clang * fix clang tests * fix pattern * add test case null * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * Update src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java Co-authored-by: Piotr Idzik <[email protected]> * rename MAP_OF_CHARS to KEYPAD * fix clang * remove main * add tests * feat: throw for wrong inputs * change keypad to list * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <[email protected]> * fix with number 1 (empty value), and add tests * style: avoid concatenation while populating `KEYPAD` * change to assertEquals --------- Co-authored-by: Samuel Facchinello <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent 31db1af commit 51fcc66

File tree

2 files changed

+71
-67
lines changed

2 files changed

+71
-67
lines changed

src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java

+46-34
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,61 @@
55
import java.util.List;
66

77
public final class LetterCombinationsOfPhoneNumber {
8+
9+
private static final char EMPTY = '\0';
10+
11+
// Mapping of numbers to corresponding letters on a phone keypad
12+
private static final String[] KEYPAD = new String[] {" ", String.valueOf(EMPTY), "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
13+
814
private LetterCombinationsOfPhoneNumber() {
915
}
1016

11-
static Character[][] numberToCharMap;
12-
13-
protected static List<String> printWords(int[] numbers, int len, int numIndex, String s) {
14-
if (len == numIndex) {
15-
return new ArrayList<>(Collections.singleton(s));
17+
/**
18+
* Generates a list of all possible letter combinations that the provided
19+
* array of numbers could represent on a phone keypad.
20+
*
21+
* @param numbers an array of integers representing the phone numbers
22+
* @return a list of possible letter combinations
23+
*/
24+
public static List<String> getCombinations(int[] numbers) {
25+
if (numbers == null) {
26+
return List.of("");
1627
}
28+
return generateCombinations(numbers, 0, new StringBuilder());
29+
}
1730

18-
List<String> stringList = new ArrayList<>();
31+
/**
32+
* Recursive method to generate combinations of letters from the phone keypad.
33+
*
34+
* @param numbers the input array of phone numbers
35+
* @param index the current index in the numbers array being processed
36+
* @param current a StringBuilder holding the current combination of letters
37+
* @return a list of letter combinations formed from the given numbers
38+
*/
39+
private static List<String> generateCombinations(int[] numbers, int index, StringBuilder current) {
40+
// Base case: if we've processed all numbers, return the current combination
41+
if (index == numbers.length) {
42+
return new ArrayList<>(Collections.singletonList(current.toString()));
43+
}
1944

20-
for (int i = 0; i < numberToCharMap[numbers[numIndex]].length; i++) {
21-
String sCopy = String.copyValueOf(s.toCharArray());
22-
sCopy = sCopy.concat(numberToCharMap[numbers[numIndex]][i].toString());
23-
stringList.addAll(printWords(numbers, len, numIndex + 1, sCopy));
45+
final var number = numbers[index];
46+
if (number < 0 || number > 9) {
47+
throw new IllegalArgumentException("Input numbers must in the range [0, 9]");
2448
}
25-
return stringList;
26-
}
2749

28-
private static void printWords(int[] numbers) {
29-
generateNumberToCharMap();
30-
List<String> stringList = printWords(numbers, numbers.length, 0, "");
31-
stringList.stream().forEach(System.out::println);
32-
}
50+
List<String> combinations = new ArrayList<>();
3351

34-
protected static void generateNumberToCharMap() {
35-
numberToCharMap = new Character[10][5];
36-
numberToCharMap[0] = new Character[] {'\0'};
37-
numberToCharMap[1] = new Character[] {'\0'};
38-
numberToCharMap[2] = new Character[] {'a', 'b', 'c'};
39-
numberToCharMap[3] = new Character[] {'d', 'e', 'f'};
40-
numberToCharMap[4] = new Character[] {'g', 'h', 'i'};
41-
numberToCharMap[5] = new Character[] {'j', 'k', 'l'};
42-
numberToCharMap[6] = new Character[] {'m', 'n', 'o'};
43-
numberToCharMap[7] = new Character[] {'p', 'q', 'r', 's'};
44-
numberToCharMap[8] = new Character[] {'t', 'u', 'v'};
45-
numberToCharMap[9] = new Character[] {'w', 'x', 'y', 'z'};
46-
}
52+
// Iterate over each letter and recurse to generate further combinations
53+
for (char letter : KEYPAD[number].toCharArray()) {
54+
if (letter != EMPTY) {
55+
current.append(letter);
56+
}
57+
combinations.addAll(generateCombinations(numbers, index + 1, current));
58+
if (letter != EMPTY) {
59+
current.deleteCharAt(current.length() - 1); // Backtrack by removing the last appended letter
60+
}
61+
}
4762

48-
// Driver code
49-
public static void main(String[] args) {
50-
int[] number = {2, 3, 4};
51-
printWords(number);
63+
return combinations;
5264
}
5365
}
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
11
package com.thealgorithms.strings;
22

3-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

5-
import java.util.Arrays;
66
import java.util.List;
7-
import org.junit.jupiter.api.Test;
7+
import java.util.stream.Stream;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
811

912
public class LetterCombinationsOfPhoneNumberTest {
1013

11-
@Test
12-
public void letterCombinationsOfPhoneNumber() {
13-
LetterCombinationsOfPhoneNumber.generateNumberToCharMap();
14-
15-
// ** Test 1 **
16-
// Input: digits = ""
17-
// Output: []
18-
int[] numbers1 = {};
19-
List<String> output1 = Arrays.asList("");
20-
assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers1, numbers1.length, 0, "").equals(output1));
14+
@ParameterizedTest
15+
@MethodSource("provideTestCases")
16+
public void testLetterCombinationsOfPhoneNumber(int[] numbers, List<String> expectedOutput) {
17+
assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers));
18+
}
2119

22-
// ** Test 2 **
23-
// Input: digits = "2"
24-
// Output: ["a","b","c"]
25-
int[] numbers2 = {2};
26-
List<String> output2 = Arrays.asList("a", "b", "c");
27-
assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers2, numbers2.length, 0, "").equals(output2));
20+
@ParameterizedTest
21+
@MethodSource("wrongInputs")
22+
void throwsForWrongInput(int[] numbers) {
23+
assertThrows(IllegalArgumentException.class, () -> LetterCombinationsOfPhoneNumber.getCombinations(numbers));
24+
}
2825

29-
// ** Test 3 **
30-
// Input: digits = "23"
31-
// Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
32-
int[] numbers3 = {2, 3};
33-
List<String> output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf");
34-
assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers3, numbers3.length, 0, "").equals(output3));
26+
private static Stream<Arguments> provideTestCases() {
27+
return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, List.of("a", "b", "c")), Arguments.of(new int[] {2, 3}, List.of("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")),
28+
Arguments.of(new int[] {2, 3, 4}, List.of("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")),
29+
Arguments.of(new int[] {3, 3}, List.of("dd", "de", "df", "ed", "ee", "ef", "fd", "fe", "ff")), Arguments.of(new int[] {8, 4}, List.of("tg", "th", "ti", "ug", "uh", "ui", "vg", "vh", "vi")), Arguments.of(new int[] {2, 0}, List.of("a ", "b ", "c ")),
30+
Arguments.of(new int[] {9, 2}, List.of("wa", "wb", "wc", "xa", "xb", "xc", "ya", "yb", "yc", "za", "zb", "zc")), Arguments.of(new int[] {0}, List.of(" ")), Arguments.of(new int[] {1}, List.of("")), Arguments.of(new int[] {2}, List.of("a", "b", "c")),
31+
Arguments.of(new int[] {1, 2, 0, 4}, List.of("a g", "a h", "a i", "b g", "b h", "b i", "c g", "c h", "c i")));
32+
}
3533

36-
// ** Test 4 **
37-
// Input: digits = "234"
38-
// Output: ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi",
39-
// "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh",
40-
// "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"]
41-
int[] numbers4 = {2, 3, 4};
42-
List<String> output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi");
43-
assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers4, numbers4.length, 0, "").equals(output4));
34+
private static Stream<Arguments> wrongInputs() {
35+
return Stream.of(Arguments.of(new int[] {-1}), Arguments.of(new int[] {10}), Arguments.of(new int[] {2, 2, -1, 0}), Arguments.of(new int[] {0, 0, 0, 10}));
4436
}
4537
}

0 commit comments

Comments
 (0)