« Return keypad code

Problem

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

Solution

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14string dial(int key)
15{
16 unordered_map<int, string> m;
17 m[0] = "";
18 m[1] = "";
19 m[2] = "abc";
20 m[3] = "def";
21 m[4] = "ghi";
22 m[5] = "jkl";
23 m[6] = "mno";
24 m[7] = "pqrs";
25 m[8] = "tuv";
26 m[9] = "wxyz";
27 return m[key];
28}
29
30int keypad(int num, string output[])
31{
32 if (num < 10)
33 {
34 string str = dial(num);
35 for (int j = 0; j < str.size(); j++)
36 {
37 output[j] = str[j];
38 }
39 return str.size();
40 }
41 int smallNum = num / 10;
42 int smallOutputSize = keypad(smallNum, output);
43 string str2 = dial(num % 10);
44 for (int j = 1; j < str2.size(); j++)
45 {
46 for (int i = 0; i < smallOutputSize; i++)
47 {
48 output[j * smallOutputSize + i] = output[i];
49 }
50 }
51 for (int j = 0; j < str2.size(); j++)
52 {
53 for (int i = 0; i < smallOutputSize; i++)
54 {
55 output[j * smallOutputSize + i] = output[j * smallOutputSize + i] + str2[j];
56 }
57 }
58 return (str2.size() * smallOutputSize);
59}
60int main()
61{
62 int num;
63 cin >> num;
64
65 string output[100000];
66 int count = keypad(num, output);
67 for (int i = 0; i < count; i++)
68 {
69 cout << output[i] << endl;
70 }
71 return 0;
72}