-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_17.java
More file actions
30 lines (25 loc) · 839 Bytes
/
Copy pathP_17.java
File metadata and controls
30 lines (25 loc) · 839 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
package leetcode.medium;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class P_17 {
static String[] s = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
public List<String> letterCombinations(String digits) {
if (digits.isEmpty()) {
return Collections.emptyList();
}
final List<String> res = new ArrayList<>();
dfs(digits.toCharArray(), 0, new char[digits.length()], res);
return res;
}
private static void dfs(char[] d, int idx, char[] curr, List<String> res) {
if (idx == d.length) {
res.add(new String(curr));
return;
}
for (char c : s[d[idx] - '0'].toCharArray()) {
curr[idx] = c;
dfs(d, idx + 1, curr, res);
}
}
}