Return Keypad Code

 Return Keypad Code

Send Feedback

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[]){
string str[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
if(num==0){
output[0]="";
return 1;
}
int lnum=num%10;
int rnum=num/10;
string smalloutput[10000];
int t = keypad(rnum,smalloutput);
int k=0;
string s = str[lnum];
int l =s.length();
for(int i=0; i<t; i++){
for(int j=0; j<l; j++){
output[k]=smalloutput[i]+s[j];
k++;
}
}
return k;
}



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


#include <string>
using namespace std;

int keypad(int num, string output[]){
string str[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
if(num==0){
output[0]="";
return 1;
}
int lnum=num%10;
int rnum=num/10;
string smalloutput[10000];
int t = keypad(rnum,smalloutput);
int k=0;
string s = str[lnum];
int l =s.length();
for(int i=0; i<t; i++){
for(int j=0; j<l; j++){
output[k]=smalloutput[i]+s[j];
k++;
}
}
return k;
}



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

string output[10000];
int count = keypad(num, output);
for(int i = 0; i < count && i < 10000; i++){
cout << output[i] << endl;
}
return 0;
}

Comments

Popular posts from this blog

Code : All connected components

Coding Ninjas