-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonenumber
More file actions
39 lines (28 loc) · 795 Bytes
/
Copy pathphonenumber
File metadata and controls
39 lines (28 loc) · 795 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
31
32
33
34
35
36
37
38
39
class Solution {
List<String> ans = new ArrayList<>();
String[] map = {
"", "", "abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz"
};
public List<String> letterCombinations(String digits) {
if(digits.length() == 0)
return ans;
backtrack(digits, 0, "");
return ans;
}
private void backtrack(String digits, int index, String current) {
if(index == digits.length()) {
ans.add(current);
return;
}
String letters = map[digits.charAt(index) - '0'];
for(int i = 0; i < letters.length(); i++) {
backtrack(
digits,
index + 1,
current + letters.charAt(i)
);
}
}
}