« Print keypad combinations code
Problem
Given an integer n, using phone keypad find out and print all the possible strings that can be made using digits of input n.
Note : The order of strings are not important. Just print different strings in new lines.
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_MIN11#include <unordered_map>12using namespace std;1314string 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}2930void printKeypad(int num, string out)31{32 if (num == 0)33 {34 cout << out << endl;35 return;36 }37 string str = dial(num % 10);38 for (int i = 0; i < str.size(); i++)39 {40 printKeypad(num / 10, str[i] + out);41 }42}4344void printKeypad(int num)45{46 printKeypad(num, "");47}4849int main()50{51 int num;52 cin >> num;53 printKeypad(num);54 return 0;55}