-
Notifications
You must be signed in to change notification settings - Fork 0
/
Letter Combinations of a Phone Number.java
39 lines (33 loc) · 1.23 KB
/
Letter Combinations of a Phone Number.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
34
35
36
37
38
39
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<String> letterCombinations(String digits) {
if (digits.length() == 0) {
return new ArrayList<>();
}
char[][] letters = new char[8][];
letters['2' - '2'] = new char[] {'a', 'b', 'c'};
letters['3' - '2'] = new char[] {'d', 'e', 'f'};
letters['4' - '2'] = new char[] {'g', 'h', 'i'};
letters['5' - '2'] = new char[] {'j', 'k', 'l'};
letters['6' - '2'] = new char[] {'m', 'n', 'o'};
letters['7' - '2'] = new char[] {'p', 'q', 'r', 's'};
letters['8' - '2'] = new char[] {'t', 'u', 'v'};
letters['9' - '2'] = new char[] {'w', 'x', 'y', 'z'};
List<String> res = new ArrayList<>();
char[] cur = new char[digits.length()];
dfs(digits, letters, res, cur, 0);
return res;
}
void dfs(String digits, char[][] letters, List<String> res, char[] cur,
int index) {
if (index == digits.length()) {
res.add(new String(cur));
return;
}
for (char c : letters[digits.charAt(index) - '2']) {
cur[index] = c;
dfs(digits, letters, res, cur, index + 1);
}
}
}