Skip to content

Commit 1e37b97

Browse files
edit 17
1 parent 86b06c1 commit 1e37b97

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

‎src/main/java/com/fishercoder/solutions/_17.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@
33
import java.util.*;
44

55
/**
6+
* 17. Letter Combinations of a Phone Number
7+
*
68
* Given a digit string, return all possible letter combinations that the number could represent.
7-
8-
A mapping of digit to letters (just like on the telephone buttons) is given below.
9+
* A mapping of digit to letters (just like on the telephone buttons) is given below.
910
1011
Input:Digit string "23"
1112
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
13+
1214
Note:
1315
Although the above answer is in lexicographical order, your answer could be in any order you want.
1416
*/
1517

1618
public class _17 {
1719

1820
public List<String> letterCombinations(String digits) {
19-
String[] digits2Letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
20-
2121
List<String> result = new ArrayList();
2222
if(digits.length() == 0) return result;
2323

24+
String[] digits2Letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
25+
2426
result.add("");//this line is important, otherwise result is empty and Java will default it to an empty String
2527
for(int i = 0; i < digits.length(); i++){
2628
result = combine(digits2Letters[digits.charAt(i)-'0'], result);
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._17;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertTrue;
13+
14+
public class _17Test {
15+
private static _17 test;
16+
private static String digits;
17+
private static List<String> expected;
18+
private static List<String> actual;
19+
20+
@BeforeClass
21+
public static void setup(){
22+
test = new _17();
23+
}
24+
25+
@Test
26+
public void test1(){
27+
digits = "2";
28+
actual = test.letterCombinations(digits);
29+
expected = new ArrayList<>(Arrays.asList("a", "b", "c"));
30+
assertEquals(expected, actual);
31+
}
32+
33+
@Test
34+
public void test2(){
35+
digits = "23";
36+
actual = test.letterCombinations(digits);
37+
expected = new ArrayList<>(Arrays.asList("ad","ae","af","bd","be","bf","cd","ce","cf"));
38+
/**order doesn't matter, so we check like below*/
39+
assertTrue(expected.containsAll(actual) && actual.containsAll(expected));
40+
}
41+
}

0 commit comments

Comments
 (0)