Print Keypad Combinations Code

 Print Keypad Combinations Code

Send Feedback

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


/*
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
string dial(int key){
unordered_map<int,string>m;
m[0]="";
m[1]="";
m[2]="abc";
m[3]="def";
m[4]="ghi";
m[5]="jkl";
m[6]="mno";
m[7]="pqrs";
m[8]="tuv";
m[9]="wxyz";
return m[key];
}
void pK(int num,string out){
if(num==0){
cout<<out<<endl;
return;
}
string pd=dial(num%10);
for(int i=0;i<pd.size();i++){
pK(num/10,pd[i]+out);
}
}

void printKeypad(int num){
pK(num,"");
}
*/


#include <string>
using namespace std;
int helper( string input[], string output[], int num ){
if( num== 0 || num==1){
output[0] = "";
return 1;
}
int val1 = num /10;
int val2 = num %10;
string smalloutput[500];
int ansf = helper( input, smalloutput, val1);
string ans = input[val2];
int k=0;
for(int i=0; i<ans.length(); i++){
for( int j=0; j <ansf; j++){
output[k] = smalloutput[j] + ans[i];
k++;
}
}
return k;
}


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[]= { "","", "abc" , "def" ,"ghi" ,"jkl" ,"mno", "pqrs", "tuv", "wxyz"};
int ans = helper( input, output, num);
return ans;
}



#include <iostream>
#include <string>
using namespace std;

void helper(int num, string a[], string output){
if( num ==0 ){
cout << output << endl;
return;
}
int last = num %10;
int first = num /10;
string smalloutput[1000];
string op= a[last];
for( int i=0; i< op.length(); i++){
helper(num/10,a,op[i] +output);
}
}
void printKeypad(int num){
/*
Given an integer number print all the possible combinations of the keypad. You do not need to return anything just print them.
*/
string a[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
helper( num, a, "");
}


#include <iostream>
#include <string>
#include "solution.h"
using namespace std;

int main(){
int num;
cin >> num;

printKeypad(num);

return 0;
}

Comments

Popular posts from this blog

Code : All connected components

Coding Ninjas