-
Notifications
You must be signed in to change notification settings - Fork 0
/
17RecursionLetterKeys.java
33 lines (33 loc) · 1.11 KB
/
17RecursionLetterKeys.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
30
31
32
33
class Solution {
private static final char[][] chars = new char[][]{
{},
{},
{'a', 'b', 'c'},
{'d', 'e', 'f'},
{'g', 'h', 'i'},
{'j', 'k', 'l'},
{'m', 'n', 'o'},
{'p', 'q', 'r', 's'},
{'t', 'u', 'v'},
{'w', 'x', 'y', 'z'},
};
//OR
//private static final String[] KEYS = {"", "" ,"abc","def","ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
//in association with KEYS.charAt(index);
public List<String> letterCombinations(String digits) {
List<String> ret = new LinkedList<>();
if(digits.length() == 0) return ret;
recursion(digits, 0, "", ret);
return ret;
}
public void recursion(String digits, int pos, String pre, List<String> list){
if(pre.length() == digits.length()){
list.add(pre);
return;
}
int index = digits.charAt(pos)-'0';
for(int i=0; i<chars[index].length; i++){
recursion(digits, pos+1, pre + chars[index][i], list);
}
}
}