-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypadProblem.java
34 lines (28 loc) · 1007 Bytes
/
keypadProblem.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
import java.util.*;
public class letterCombination {
static String keypadKeys[] = {"","v" , "abc" , "def","ghi" , "jkl","mno" ,"pqrs","tuv","wxyz"};
static ArrayList<String> keypad (String number){
// base case
if(number.length() == 0){
ArrayList<String> empList = new ArrayList<>();
empList.add("");
return empList;
}
char currentChar = number.charAt(0); // for 2 index
String remString = number.substring(1); // for 3 index
int index = currentChar - '0'; // convert into integer
String key = keypadKeys[index];
ArrayList<String> result = new ArrayList<>();
for(int i=0;i<key.length();i++){
ArrayList<String> temp = keypad(remString);
for(String s : temp){
result.add(key.charAt(i) + s);
}
}
return result;
}
public static void main(String[] args){
ArrayList<String> list = keypad("23");
System.out.println(list);
}
}