-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursionKeyPadCombination.java
43 lines (33 loc) · 1.19 KB
/
recursionKeyPadCombination.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
35
36
37
38
39
40
41
42
43
import java.util.ArrayList;
import java.util.Scanner;
public class recursionKeyPadCombination {
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
String str = scn.next();
ArrayList<String> fRes = getKPC(str);
System.out.println(fRes);
}
// global varible creation - Static keyword
static String[] codes = {".;", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tu", "vwx", "yx"};
public static ArrayList<String> getKPC(String str){
//base case
if(str.length() == 0){
ArrayList<String> bres = new ArrayList<>();
bres.add("");
return bres;
}
//recusion code
char ch = str.charAt(0);
String restS = str.substring(1);
ArrayList<String> oldRes = getKPC(restS);
ArrayList<String> newRes = new ArrayList<>();
String codeforch = codes[ch - '0'];
for(int i = 0; i<codeforch.length(); i++){
char onecharfromCodeforCh = codeforch.charAt(i);
for(String one : oldRes){
newRes.add(onecharfromCodeforCh + one);
}
}
return newRes;
}
}