-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathReturnKeypadCode.cpp
76 lines (69 loc) · 1.84 KB
/
ReturnKeypadCode.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
/*Given an integer n, using phone keypad find out all the possible strings that can be made using digits of input n.
Return empty string for numbers 0 and 1.
Note : 1. The order of strings are not important.
2. Input and output has already been managed for you. You just have to populate the output array and return the count of elements populated in the output array.
Input Format :
Integer n
Output Format :
All possible strings in different lines
Constraints :
1 <= n <= 10^6
Sample Input:
23
Sample Output:
ad
ae
af
bd
be
bf
cd
ce
cf
*/
#include <string>
using namespace std;
int keypad(int num, string output[]){
/* Insert all the possible combinations of the integer number into the output string array. You do not need to
print anything, just return the number of strings inserted into the array.
*/
string input;
if(num == 0){
output[0] = "";
return 1;
}
int n = num%10;
num = num/10;
int smalloutputsize = keypad(num, output);
switch(n){
case 2: input = "abc";
break;
case 3: input = "def";
break;
case 4: input = "ghi";
break;
case 5: input = "jkl";
break;
case 6: input = "mno";
break;
case 7: input = "pqrs";
break;
case 8: input = "tuv";
break;
case 9: input = "wxyz";
break;
}
int ans_size=smalloutputsize*(input.size());
string temp[ans_size];
int k=0;
for(int i=0; i<smalloutputsize; i++){
for(int j=0; j<input.size(); j++){
temp[k] = output[i]+input[j];
k++;
}
}
for(int i=0; i<ans_size; i++){
output[i] = temp[i];
}
return input.size()*smalloutputsize;
}