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.

TITLE:-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:-

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

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.
    */
    if(num <0)
        return -1;
    if(num==0||num==1)
        {
        output[0]="";
        return 1;
        }
    int smallnum=num/10;
    int renum=num%10;
    int smallcount=keypad(smallnum,output);
    int vo;

    string s[]={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    if(renum==7||renum==9)
        vo=4;
    else
        vo=3;
    int j=0;
    int c=0;
    string output1[10000];
    while(j<vo)
    {
        for(int i=0;i<smallcount;i++)
        {
            output1[c]=output[i] +s[renum-2][j];
            c++;
        }
        j++;

    }
    for(int i=0;i<c;i++)
    {
        output[i]=output1[i];
    }
    return vo*smallcount;
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *