|
| 1 | +/*Given an integer n, using phone keypad find out all the possible strings that can be made using digits of input n. |
| 2 | +Return empty string for numbers 0 and 1. |
| 3 | +Note : 1. The order of strings are not important. |
| 4 | +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. |
| 5 | +Input Format : |
| 6 | +Integer n |
| 7 | +Output Format : |
| 8 | +All possible strings in different lines |
| 9 | +Constraints : |
| 10 | +1 <= n <= 10^6 |
| 11 | +Sample Input: |
| 12 | +23 |
| 13 | +Sample Output: |
| 14 | +ad |
| 15 | +ae |
| 16 | +af |
| 17 | +bd |
| 18 | +be |
| 19 | +bf |
| 20 | +cd |
| 21 | +ce |
| 22 | +cf |
| 23 | +*/ |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +#include <string> |
| 28 | +using namespace std; |
| 29 | + |
| 30 | +int keypad(int num, string output[]){ |
| 31 | + /* Insert all the possible combinations of the integer number into the output string array. You do not need to |
| 32 | + print anything, just return the number of strings inserted into the array. |
| 33 | + */ |
| 34 | + string input; |
| 35 | + if(num == 0){ |
| 36 | + output[0] = ""; |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + int n = num%10; |
| 41 | + num = num/10; |
| 42 | + int smalloutputsize = keypad(num, output); |
| 43 | + switch(n){ |
| 44 | + case 2: input = "abc"; |
| 45 | + break; |
| 46 | + case 3: input = "def"; |
| 47 | + break; |
| 48 | + case 4: input = "ghi"; |
| 49 | + break; |
| 50 | + case 5: input = "jkl"; |
| 51 | + break; |
| 52 | + case 6: input = "mno"; |
| 53 | + break; |
| 54 | + case 7: input = "pqrs"; |
| 55 | + break; |
| 56 | + case 8: input = "tuv"; |
| 57 | + break; |
| 58 | + case 9: input = "wxyz"; |
| 59 | + break; |
| 60 | + |
| 61 | + } |
| 62 | + int ans_size=smalloutputsize*(input.size()); |
| 63 | + string temp[ans_size]; |
| 64 | + int k=0; |
| 65 | + for(int i=0; i<smalloutputsize; i++){ |
| 66 | + for(int j=0; j<input.size(); j++){ |
| 67 | + temp[k] = output[i]+input[j]; |
| 68 | + k++; |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + for(int i=0; i<ans_size; i++){ |
| 73 | + output[i] = temp[i]; |
| 74 | + } |
| 75 | + return input.size()*smalloutputsize; |
| 76 | +} |
0 commit comments