« Print all codes in 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.
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
Solution
1#include <iostream>2#include <iomanip>3#include <algorithm>4#include <string>5#include <cstring>6#include <vector>7#include <cmath>8#include <map>9#include <climits>10// climits for INT_MIN11#include <unordered_map>12using namespace std;1314void printAllPossibleCodesHelper(string input, string output)15{16 if (input.size() == 0)17 {18 cout << output << endl;19 return;20 }2122 char c1 = (input[0] - 48) + 96;23 printAllPossibleCodesHelper(input.substr(1), output + c1);2425 if (input.size() > 1)26 {27 int d = (input[0] - 48) * 10 + (input[1] - 48);28 if (d <= 26)29 {30 char c2 = 96 + d;31 printAllPossibleCodesHelper(input.substr(2), output + c2);32 }33 }34}3536void printAllPossibleCodes(string input)37{38 printAllPossibleCodesHelper(input, "");39}4041int main()42{43 string input;44 cin >> input;4546 printAllPossibleCodes(input);47 return 0;48}