Print all Codes – String
PROBLEM:-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.
A numeric string S
All possible codes in different lines
Constraints :
1 <= Length of String S <= 10
1123
Sample Output:
aabc
kbc
alc
aaw
kw
SOLUTION:-
#include <iostream>
#include <string.h>
using namespace std;
int atoi(char a)
{
int i=a-'0';
return i;
}
char itoa(int i)
{
char c='a'+i-1;
return c;}
void P(string input,string output){
if(input.empty())
{
cout<<output<<endl;
return;
}
P(input.substr(1),output+itoa(atoi(input[0])));
int f=atoi(input[0]);
int s=atoi(input[1]);
int fs=s+(f*10);
if(fs>10&&fs<=26)
P(input.substr(2),output+itoa(fs));
return;
}
void printAllPossibleCodes(string input) {
/*
Given the input as a string, print all its possible combinations. You do not need to return anything.
*/
P(input,"");
return;
}
int main(){
string input;
cin >> input;
printAllPossibleCodes(input);
return 0;
}