Skip to content

Commit 788a822

Browse files
authored
Update readme.md
1 parent 8f98353 commit 788a822

File tree

1 file changed

+74
-1
lines changed
  • src/main/java/g0001_0100/s0017_letter_combinations_of_a_phone_number

1 file changed

+74
-1
lines changed

src/main/java/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,77 @@ A mapping of digits to letters (just like on the telephone buttons) is given bel
2323
**Constraints:**
2424

2525
* `1 <= digits.length <= 4`
26-
* `digits[i]` is a digit in the range `['2', '9']`.
26+
* `digits[i]` is a digit in the range `['2', '9']`.
27+
28+
To solve the Letter Combinations of a Phone Number problem in Java using a `Solution` class, we'll follow these steps:
29+
30+
1. Define a `Solution` class with a method named `letterCombinations` that takes a string `digits` as input and returns a list of all possible letter combinations.
31+
2. Create a mapping of digits to letters using a hashmap or an array.
32+
3. Initialize an empty list `result` to store the combinations.
33+
4. If the input string `digits` is empty, return an empty list `result`.
34+
5. Call a recursive function `generateCombinations` to generate combinations for each digit.
35+
6. Within the recursive function:
36+
- Base case: If the current combination length equals the length of the input `digits`, add the combination to the `result` list.
37+
- Recursive step: For the current digit, iterate over its corresponding letters and append each letter to the current combination, then recursively call the function with the next digit.
38+
7. Return the `result` list containing all possible combinations.
39+
40+
Here's the implementation:
41+
42+
```java
43+
import java.util.ArrayList;
44+
import java.util.HashMap;
45+
import java.util.List;
46+
import java.util.Map;
47+
48+
public class Solution {
49+
private static final Map<Character, String> digitToLetters = new HashMap<>();
50+
static {
51+
digitToLetters.put('2', "abc");
52+
digitToLetters.put('3', "def");
53+
digitToLetters.put('4', "ghi");
54+
digitToLetters.put('5', "jkl");
55+
digitToLetters.put('6', "mno");
56+
digitToLetters.put('7', "pqrs");
57+
digitToLetters.put('8', "tuv");
58+
digitToLetters.put('9', "wxyz");
59+
}
60+
61+
public List<String> letterCombinations(String digits) {
62+
List<String> result = new ArrayList<>();
63+
if (digits.length() == 0) {
64+
return result;
65+
}
66+
generateCombinations(result, digits, "", 0);
67+
return result;
68+
}
69+
70+
private void generateCombinations(List<String> result, String digits, String combination, int index) {
71+
if (index == digits.length()) {
72+
result.add(combination);
73+
return;
74+
}
75+
76+
char digit = digits.charAt(index);
77+
String letters = digitToLetters.get(digit);
78+
for (char letter : letters.toCharArray()) {
79+
generateCombinations(result, digits, combination + letter, index + 1);
80+
}
81+
}
82+
83+
public static void main(String[] args) {
84+
Solution solution = new Solution();
85+
86+
// Test cases
87+
String digits1 = "23";
88+
System.out.println("Example 1 Output: " + solution.letterCombinations(digits1));
89+
90+
String digits2 = "";
91+
System.out.println("Example 2 Output: " + solution.letterCombinations(digits2));
92+
93+
String digits3 = "2";
94+
System.out.println("Example 3 Output: " + solution.letterCombinations(digits3));
95+
}
96+
}
97+
```
98+
99+
This implementation provides a solution to the Letter Combinations of a Phone Number problem in Java.

0 commit comments

Comments
 (0)