-
Notifications
You must be signed in to change notification settings - Fork 52
/
LetterCombinationsOfPhoneNumber.java
48 lines (44 loc) · 1.57 KB
/
LetterCombinationsOfPhoneNumber.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
40
41
42
43
44
45
46
47
48
import java.util.ArrayList;
import java.util.List;
class Solution {
// Mapping of digits to letters as per telephone keypad
private static final String[] KEYPAD = {
"", // 0
"", // 1
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs", // 7
"tuv", // 8
"wxyz" // 9
};
public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<>();
// If input is empty, return an empty list
if (digits == null || digits.isEmpty()) {
return result;
}
// Start backtracking to generate all combinations
backtrack(result, digits, new StringBuilder(), 0);
return result;
}
// Recursive helper function to generate combinations
private void backtrack(List<String> result, String digits, StringBuilder current, int index) {
// If the current combination is complete, add to result list
if (index == digits.length()) {
result.add(current.toString());
return;
}
// Get the possible letters for the current digit
String letters = KEYPAD[digits.charAt(index) - '0'];
for (char letter : letters.toCharArray()) {
// Append the letter and move to the next digit
current.append(letter);
backtrack(result, digits, current, index + 1);
// Backtrack: remove the last letter to try other combinations
current.deleteCharAt(current.length() - 1);
}
}
}