-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw_LetterCombination.java
More file actions
39 lines (34 loc) · 1.28 KB
/
hw_LetterCombination.java
File metadata and controls
39 lines (34 loc) · 1.28 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
import java.util.ArrayList;
import java.util.List;
public class hw_LetterCombination {
static String keys[] = {"", ".", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public static List<String> letterCombinations(String digits) {
// Termination case
if(digits.length() == 0) {
List<String> list = new ArrayList<>();
list.add("");
return list;
}
// Get one digit at a time
// if digit is "23", then firstchar will store '2'
char firstChar = digits.charAt(0);
// convert firstchar into int
int index = firstChar - '0';
// currentKey will store element at index i.e., "abc"
String currentKey = keys[index];
String remainingString = digits.substring(1);
List<String> ans = new ArrayList<>();
for(int i = 0; i < currentKey.length(); i++) {
List<String> res = letterCombinations(remainingString);
for(String s : res) {
ans.add(currentKey.charAt(i) + s);
}
}
return ans;
}
public static void main(String[] args) {
String digits = "23";
List<String> op = letterCombinations(digits);
System.out.println(op);
}
}