Skip to content

Commit c8ca38e

Browse files
update 17
1 parent 278e1c5 commit c8ca38e

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,33 @@ List<String> combine(String letters, List<String> result) {
3434
return newResult;
3535
}
3636
}
37+
38+
public static class Solution2 {
39+
/**
40+
* My completely solution on 10/11/2021, no backtracking involved.
41+
*/
42+
public List<String> letterCombinations(String digits) {
43+
List<String> ans = new ArrayList<>();
44+
if (digits.length() == 0 || digits.equals("")) {
45+
return ans;
46+
}
47+
String[] options = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
48+
ans.add("");
49+
return recursion(ans, options, digits, 0);
50+
}
51+
52+
private List<String> recursion(List<String> ans, String[] options, String digits, int index) {
53+
if (index >= digits.length()) {
54+
return ans;
55+
}
56+
List<String> newAns = new ArrayList<>();
57+
String candidates = options[Integer.parseInt(digits.charAt(index) + "")];
58+
for (String str : ans) {
59+
for (int i = 0; i < candidates.length(); i++) {
60+
newAns.add(str + candidates.charAt(i));
61+
}
62+
}
63+
return recursion(newAns, options, digits, index + 1);
64+
}
65+
}
3766
}

src/test/java/com/fishercoder/_17Test.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,35 @@
88
import java.util.Arrays;
99
import java.util.List;
1010

11+
import static org.assertj.core.api.Assertions.assertThat;
1112
import static org.junit.Assert.assertEquals;
1213
import static org.junit.Assert.assertTrue;
1314

1415
public class _17Test {
1516
private static _17.Solution1 solution1;
17+
private static _17.Solution2 solution2;
1618
private static String digits;
1719
private static List<String> expected;
18-
private static List<String> actual;
1920

2021
@BeforeClass
2122
public static void setup() {
2223
solution1 = new _17.Solution1();
24+
solution2 = new _17.Solution2();
2325
}
2426

2527
@Test
2628
public void test1() {
2729
digits = "2";
28-
actual = solution1.letterCombinations(digits);
2930
expected = new ArrayList<>(Arrays.asList("a", "b", "c"));
30-
assertEquals(expected, actual);
31+
assertThat(expected).hasSameElementsAs(solution1.letterCombinations(digits));
32+
assertThat(expected).hasSameElementsAs(solution2.letterCombinations(digits));
3133
}
3234

3335
@Test
3436
public void test2() {
3537
digits = "23";
36-
actual = solution1.letterCombinations(digits);
3738
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));
39+
assertThat(expected).hasSameElementsAs(solution1.letterCombinations(digits));
40+
assertThat(expected).hasSameElementsAs(solution2.letterCombinations(digits));
4041
}
4142
}

0 commit comments

Comments
 (0)