-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17Solution.java
29 lines (28 loc) · 896 Bytes
/
17Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//17. Letter Combinations of a Phone Number
class Solution {
// Assumption: only digits input
private static String[] keys = {"","", "abc", "def","ghi","jkl", "mno", "pqrs", "tuv", "wxyz"} ;
public List<String> letterCombinations(String digits) {
List<String>result = new ArrayList<>();
if (digits == null || digits.length() == 0) {
return result;
}
helper(result, "", 0, digits);
return result;
}
private void helper(List<String>result, String combo, int startIdx, String digits) {
//base case:
if (combo.length() == digits.length()) {
result.add(combo);
return;
}
String letters = keys[(digits.charAt(startIdx)-'0')];
for (int i = 0; i < letters.length(); i++) {
combo += letters.charAt(i);
//!!! startIdx + 1
helper(result, combo, startIdx + 1, digits);
// remove the last digit
combo = combo.substring(0, combo.length() - 1);
}
}
}