-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLetterCombination.java
More file actions
39 lines (30 loc) · 874 Bytes
/
LetterCombination.java
File metadata and controls
39 lines (30 loc) · 874 Bytes
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;
public class LetterCombination {
static String keyPadKeys[] = {
".", "@#$", "abc", "def",
"ghi", "jkl", "mno", "pqrs",
"tuv", "wxyz"
};
static ArrayList<String> combination(String number) {
if(number.length() == 0) {
ArrayList<String> list = new ArrayList<>();
list.add("");
return list;
}
char fnum = number.charAt(0); // '2'
String remString = number.substring(1); // "3"
int index = fnum - '0'; // 50 - 48
String key = keyPadKeys[index]; // "abc"
ArrayList<String> result = new ArrayList<>();
for(int i = 0; i < key.length(); i++) {
ArrayList<String> temp = combination(remString);
for(String str : temp) {
result.add(key.charAt(i) + str);
}
}
return result;
}
public static void main(String[] args) {
combination("23");
}
}