-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeypadCombination.java
53 lines (41 loc) · 1.46 KB
/
KeypadCombination.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
44
45
46
47
48
49
50
51
52
53
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/*
Name: Get KeyPad Combination (Using Recursion)
Source: PepCoding
Link: https://www.pepcoding.com/resources/online-java-foundation/recursion-with-arraylists/get-kpc-official/ojquestion
Statement: WAP to get the list of all words that could be produced by the keys in str.
*/
public class KeypadCombination {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
ArrayList<String> res = getKPC(str);
System.out.println(res);
}
public static ArrayList<String> getKPC(String str) {
ArrayList<String> keypad = new ArrayList<>();
if(str.length()==0)
{
ArrayList<String> base = new ArrayList<>();
base.add("");
return base;
}
String [] items = new String[]{".;", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tu", "vwx", "yz"};
keypad.addAll(Arrays.asList(items));
char ch = str.charAt(0);
String rest = str.substring(1);
ArrayList<String> list = getKPC(rest);
ArrayList<String> result = new ArrayList<>();
String letter = keypad.get(ch-'0');
for(int i =0; i<letter.length(); i++)
{
for( String res: list)
{
result.add(letter.charAt(i)+res);
}
}
return result;
}
}