Skip to content

Commit 6e6e71c

Browse files
authored
Create smartkeypad1.java
You will be given a numeric string S. Print all the possible codes for S. Following vector contains the codes corresponding to the digits mapped. string table[] = { " ", ".+@$", "abc", "def", "ghi", "jkl" , "mno", "pqrs" , "tuv", "wxyz" }; For example, string corresponding to 0 is " " and 1 is ".+@$" Input Format A single string containing numbers only. Constraints length of string <= 10 Output Format All possible codes one per line in the following order. The letter that appears first in the code should come first Sample Input 12 Sample Output .a .b .c +a +b +c @A @b @c $a $b $c Explanation For code 1 the corresponding string is .+@$ and abc corresponds to code 2.
1 parent 33f7f42 commit 6e6e71c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Recursion/smartkeypad1.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
public class Main {
3+
static String[] mapping = { " ", ".+@$", "abc", "def", "ghi", "jkl" , "mno", "pqrs" , "tuv", "wxyz" };
4+
public static void keypad(String digits, String comb) {
5+
6+
if (digits.length() == 0) {
7+
System.out.println(comb);
8+
return;
9+
}
10+
char digitPressed = digits.charAt(0);
11+
int index = digitPressed - '0';
12+
13+
String mappedString = mapping[index];
14+
15+
for (int i = 0; i < mappedString.length(); i++) {
16+
17+
String leftString = digits.substring(1);
18+
char choice = mappedString.charAt(i);
19+
keypad(leftString, comb + choice);
20+
}
21+
22+
}
23+
24+
25+
public static void main(String args[]) {
26+
Scanner sc = new Scanner(System.in);
27+
String str = sc.nextLine();
28+
keypad(str,"");
29+
}
30+
}

0 commit comments

Comments
 (0)