-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (28 loc) · 821 Bytes
/
Solution.java
File metadata and controls
30 lines (28 loc) · 821 Bytes
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
class Solution {
private String[] digitsMap = new String[]{
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<String>();
if (!"".equals(digits)) backTrack(0, digits, "", res);
return res;
}
public void backTrack(int beg, String digits, String temp, List<String> res) {
if (beg == digits.length()) {
res.add(temp);
} else {
String chars = digitsMap[digits.charAt(beg) - '0' - 2];
for (char c : chars.toCharArray()) {
String t = temp + String.valueOf(c);
backTrack(beg + 1, digits, t, res);
}
}
}
}