-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeypadCombinations.java
More file actions
50 lines (41 loc) · 1.48 KB
/
KeypadCombinations.java
File metadata and controls
50 lines (41 loc) · 1.48 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
47
48
49
50
import java.util.*;
public class KeypadCombinations {
// Mapping of digits to letters
private static final Map<Character, String> keypadMap = Map.of(
'2', "abc",
'3', "def",
'4', "ghi",
'5', "jkl",
'6', "mno",
'7', "pqrs",
'8', "tuv",
'9', "wxyz"
);
public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<>();
if (digits == null || digits.length() == 0) {
return result;
}
backtrack(result, new StringBuilder(), digits, 0);
return result;
}
private void backtrack(List<String> result, StringBuilder current, String digits, int index) {
if (index == digits.length()) {
result.add(current.toString());
return;
}
String possibleLetters = keypadMap.get(digits.charAt(index));
for (char c : possibleLetters.toCharArray()) {
current.append(c);
backtrack(result, current, digits, index + 1);
current.deleteCharAt(current.length() - 1); // backtrack
}
}
// Test
public static void main(String[] args) {
KeypadCombinations generator = new KeypadCombinations();
System.out.println(generator.letterCombinations("23"));
System.out.println(generator.letterCombinations(""));
System.out.println(generator.letterCombinations("2"));
}
}