Print all Codes - String

 Print all Codes - String

Send Feedback

Assume that the value of a = 1, b = 2, c = 3, ... , z = 26. You are given a numeric string S. Write a program to print the list of all possible codes that can be generated from the given string.

Note : The order of codes are not important. Just print them in different lines.
Input format :
A numeric string S
Output Format :
All possible codes in different lines
Constraints :

1 <= Length of String S <= 10

Sample Input:
1123
Sample Output:
aabc
kbc
alc
aaw
kw

#include <string.h>
using namespace std;

char findCharacter(int n)
{
char letter='a'+n-1;
return letter;
}

void printAllPossibleCodeHelper(string input,string output)
{
if(input.empty())
{
cout<<output<<endl;
return;
}
int digit;
digit=input[0]-'0';
char letter;
letter=findCharacter(digit);
printAllPossibleCodeHelper(input.substr(1),output+letter);
if(input.size()>=2)
{
digit=(input[0]-'0')*10 + (input[1]-'0');
if(digit>=10 && digit<=26)
{
letter=findCharacter(digit);
printAllPossibleCodeHelper(input.substr(2),output+letter);
}
}

}

void printAllPossibleCodes(string input) {
printAllPossibleCodeHelper(input,"");
/*
Given the input as a string, print all its possible combinations. You do not need to return anything.
*/
}





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

int main(){
string input;
cin >> input;

printAllPossibleCodes(input);
return 0;
}

Comments

Popular posts from this blog

Code : All connected components

Coding Ninjas