Skip to content

Commit eecec0f

Browse files
Add test case for LetterCombinationsOfPhoneNumber (TheAlgorithms#3662)
1 parent c9e1d96 commit eecec0f

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/main/java/com/thealgorithms/strings/List_all_Possible_Words_From_Phone_Digits.java renamed to src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
import java.util.*;
44

5-
public class List_all_Possible_Words_From_Phone_Digits {
5+
public class LetterCombinationsOfPhoneNumber {
66

77
static Character[][] numberToCharMap;
88

9-
private static List<String> printWords(
10-
int[] numbers,
11-
int len,
12-
int numIndex,
13-
String s
14-
) {
9+
protected static List<String> printWords(int[] numbers, int len, int numIndex, String s) {
1510
if (len == numIndex) {
1611
return new ArrayList<>(Collections.singleton(s));
1712
}
@@ -33,7 +28,7 @@ private static void printWords(int[] numbers) {
3328
stringList.stream().forEach(System.out::println);
3429
}
3530

36-
private static void generateNumberToCharMap() {
31+
protected static void generateNumberToCharMap() {
3732
numberToCharMap = new Character[10][5];
3833
numberToCharMap[0] = new Character[] { '\0' };
3934
numberToCharMap[1] = new Character[] { '\0' };
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.strings;
2+
3+
import java.util.*;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class LetterCombinationsOfPhoneNumberTest {
8+
9+
@Test
10+
public void letterCombinationsOfPhoneNumber() {
11+
LetterCombinationsOfPhoneNumber ob = new LetterCombinationsOfPhoneNumber();
12+
ob.generateNumberToCharMap();
13+
14+
// ** Test 1 **
15+
// Input: digits = ""
16+
// Output: []
17+
int[] numbers1 = {};
18+
List<String> output1 = Arrays.asList("");
19+
assertTrue(ob.printWords(numbers1, numbers1.length, 0, "").equals(output1));
20+
21+
// ** Test 2 **
22+
// Input: digits = "2"
23+
// Output: ["a","b","c"]
24+
int[] numbers2 = { 2 };
25+
List<String> output2 = Arrays.asList("a", "b", "c");
26+
assertTrue(ob.printWords(numbers2, numbers2.length, 0, "").equals(output2));
27+
28+
// ** Test 3 **
29+
// Input: digits = "23"
30+
// Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
31+
int[] numbers3 = { 2, 3 };
32+
List<String> output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf");
33+
assertTrue(ob.printWords(numbers3, numbers3.length, 0, "").equals(output3));
34+
35+
// ** Test 4 **
36+
// Input: digits = "234"
37+
// Output: ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi",
38+
// "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh",
39+
// "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"]
40+
int[] numbers4 = { 2, 3, 4 };
41+
List<String> output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg",
42+
"bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg",
43+
"cfh", "cfi");
44+
assertTrue(ob.printWords(numbers4, numbers4.length, 0, "").equals(output4));
45+
}
46+
}

0 commit comments

Comments
 (0)