-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_17_letterCombinationOfPhoneNumber.java
More file actions
49 lines (39 loc) · 1.37 KB
/
_17_letterCombinationOfPhoneNumber.java
File metadata and controls
49 lines (39 loc) · 1.37 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _17_letterCombinationOfPhoneNumber {
public static List<String> letterCombination(String digits){
List<String> result = new ArrayList<>();
if (digits == null || digits.length() == 0) {
return result;
}
Map<Character, String> map = new HashMap<>();
map.put('2', "abc");
map.put('3', "def");
map.put('4', "ghi");
map.put('5', "jkl");
map.put('6', "mno");
map.put('7', "pqrs");
map.put('8', "tuv");
map.put('9', "wxyz");
backTrack(result,map,digits,0,new StringBuilder());
return result;
}
public static void backTrack(List<String> result, Map<Character,String> map, String digits, int index, StringBuilder current){
if (index == digits.length()) {
result.add(current.toString());
return;
}
String letters = map.get(digits.charAt(index));
for(char c : letters.toCharArray()){
current.append(c);
backTrack(result, map, digits, index + 1, current);
current.deleteCharAt(current.length()-1); // backtrack
}
}
public static void main(String[] args) {
List<String> rs = letterCombination("23");
System.out.println(rs);
}
}