Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3d1ba93

Browse files
committedMay 9, 2022
add a solution for 17
1 parent 446679a commit 3d1ba93

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed
 

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ List<String> combine(String letters, List<String> result) {
3737

3838
public static class Solution2 {
3939
/**
40-
* My completely solution on 10/11/2021, no backtracking involved.
40+
* My completely original solution on 10/11/2021, no backtracking involved.
4141
*/
4242
public List<String> letterCombinations(String digits) {
4343
List<String> ans = new ArrayList<>();
@@ -63,4 +63,29 @@ private List<String> recursion(List<String> ans, String[] options, String digits
6363
return recursion(newAns, options, digits, index + 1);
6464
}
6565
}
66+
67+
public static class Solution3 {
68+
/**
69+
* My completely original solution on 5/9/2022, no backtracking involved or helper method involved.
70+
*/
71+
public List<String> letterCombinations(String digits) {
72+
List<String> ans = new ArrayList<>();
73+
if (digits.equals("")) {
74+
return ans;
75+
}
76+
String[] buttons = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
77+
ans.add("");
78+
for (char c : digits.toCharArray()) {
79+
String button = buttons[Integer.parseInt(c + "")];
80+
List<String> newList = new ArrayList<>();
81+
for (String str : ans) {
82+
for (char b : button.toCharArray()) {
83+
newList.add(str + b);
84+
}
85+
}
86+
ans = newList;
87+
}
88+
return ans;
89+
}
90+
}
6691
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
import java.util.List;
1010

1111
import static org.assertj.core.api.Assertions.assertThat;
12-
import static org.junit.Assert.assertEquals;
13-
import static org.junit.Assert.assertTrue;
1412

1513
public class _17Test {
1614
private static _17.Solution1 solution1;
1715
private static _17.Solution2 solution2;
16+
private static _17.Solution3 solution3;
1817
private static String digits;
1918
private static List<String> expected;
2019

2120
@BeforeClass
2221
public static void setup() {
2322
solution1 = new _17.Solution1();
2423
solution2 = new _17.Solution2();
24+
solution3 = new _17.Solution3();
2525
}
2626

2727
@Test
@@ -30,6 +30,7 @@ public void test1() {
3030
expected = new ArrayList<>(Arrays.asList("a", "b", "c"));
3131
assertThat(expected).hasSameElementsAs(solution1.letterCombinations(digits));
3232
assertThat(expected).hasSameElementsAs(solution2.letterCombinations(digits));
33+
assertThat(expected).hasSameElementsAs(solution3.letterCombinations(digits));
3334
}
3435

3536
@Test
@@ -38,5 +39,6 @@ public void test2() {
3839
expected = new ArrayList<>(Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"));
3940
assertThat(expected).hasSameElementsAs(solution1.letterCombinations(digits));
4041
assertThat(expected).hasSameElementsAs(solution2.letterCombinations(digits));
42+
assertThat(expected).hasSameElementsAs(solution3.letterCombinations(digits));
4143
}
4244
}

0 commit comments

Comments
 (0)
Failed to load comments.