-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCombOfPhoneNumber.java
More file actions
46 lines (39 loc) · 1.32 KB
/
LetterCombOfPhoneNumber.java
File metadata and controls
46 lines (39 loc) · 1.32 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
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Leetcode #17, Letter Combinations of a Phone Number
* https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
*/
public class LetterCombOfPhoneNumber {
private ArrayList<String> res = new ArrayList<>();
private Map<Character, String> map = Map.of(
'2', "abc", '3', "def", '4', "ghi", '5', "jkl",
'6', "mno", '7', "pqrs", '8', "tuv", '9', "wxyz");
private String input;
public static void main(String[] args) {
String digits = "23";
LetterCombOfPhoneNumber obj = new LetterCombOfPhoneNumber();
System.out.println(obj.letterCombinations(digits));
}
public List<String> letterCombinations(String digits) {
if (digits.length() == 0) {
return res;
}
input = digits;
backtrack(new StringBuilder(), 0);
return res;
}
public void backtrack(StringBuilder sb, int index) {
if (index == input.length()) {
res.add(sb.toString());
return;
}
String s = map.get(input.charAt(index));
for (int i = 0; i < s.length(); i++) {
sb.append(s.charAt(i));
backtrack(sb, index + 1);
sb.deleteCharAt(sb.length() - 1);
}
}
}