-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetter.java
More file actions
36 lines (32 loc) · 1.21 KB
/
Letter.java
File metadata and controls
36 lines (32 loc) · 1.21 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
import java.util.*;
public class Letter {
// Create a function to get all possible combinations of letters
public void rec(int i, String digits, String map[], String res, List<String> ans) {
if (i == digits.length()) {
ans.add(res);
return;
}
char ch = digits.charAt(i);
int num = ch - '0';
String st = map[num];
for (int j = 0; j < st.length(); j++) {
rec(i + 1, digits, map, res + st.charAt(j), ans);
}
}
public List<String> letterCombinations(String digits) {
List<String> ans = new ArrayList<>();
// Base case
if (digits.length() == 0) {
return ans;
}
String map[] = { " ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
rec(0, digits, map, "", ans);
return ans;
}
public static void main(String[] args) {
Letter letter = new Letter(); // Create an instance of the Letter class
String digits = "23"; // Example input
List<String> ans = letter.letterCombinations(digits); // Call the method
System.out.println("Combinations: " + ans); // Print the result
}
}