-
Notifications
You must be signed in to change notification settings - Fork 54
/
Return Keypad.cpp
89 lines (78 loc) · 1.53 KB
/
Return Keypad.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Given an integer n, using phone keypad find out all the possible strings that can be made using digits of input n.
The numbers and their corresponding codes are given below:
0: ""
1: ""
2: "abc"
3: "def"
4: "ghi"
5: "jkl"
6: "mno"
7: "pqrs"
8: "tuv"
9: "wxyz"
Return empty string for numbers 0 and 1.
Note:
1. The order of strings are not important.
2. The input number will have digits between: [2, 9].
Input Format :
First line of input will contain T number of test cases.
Each input consists of a single line containing an integer n.
Output Format :
For each test case, print all possible strings in different lines.
Constraints :
1 <= T <= 100
1 <= n <= 10^6
Sample Input:
1
23
Sample Output:
ad
ae
af
bd
be
bf
cd
ce
cf
Code
#include <iostream>
#include <string>
using namespace std;
int keypad(int num, string output[])
{
string key[10] = { "", "", "abc", "def", "ghi", "jkl", "mno" ,"pqrs", "tuv", "wxyz"};
if(num==0)
{
output[0]="";
return 1;
}
int digit = num % 10;
string smalloutput[1000];
int count = keypad(num/10, smalloutput);
string str=key[digit];
int k=0;
for(int i=0; i<str.length(); i++)
{
for(int j=0; j<count; j++)
{
output[k]=smalloutput[j]+str[i];
k++;
}
}
return k;
int main(){
int t;
cin >> t;
while(t--)
{
int num;
cin >> num;
string output[10000];
int count = keypad(num, output);
for(int i = 0; i < count && i < 10000; i++){
cout << output[i] << endl;
}
}
return 0;
}