Display the number of twins , Print the number after converting it into an integer, and Print the total number of distinct ways you can climb to the top.

Recursion related some problems in c++ coding

Display the number of twins , Print the number after converting it into an integer, and Print the total number of distinct ways you can climb to the top.

Share this article:

Share on Facebook Share on Twitter Share on LinkedIn Share on WhatsApp

Take as input str, a string. Assume that value of a=1, b=2, c=3, d=4, …. z=26. Write a recursive function (return type Arraylist) to print all possible codes for the string. E.g. for “1123” possible codes are aabc, kbc, alc, aaw, kw.

Input Format

Enter a number

Constraints

None

Output Format

Display all the possible codes Sample

Input

1125

Sample Output

[aabe, aay, ale, kbe, ky] in c+++ without vector use

 

Here's a possible implementation of the recursive function in C++ without using vectors:


 

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

void printCodes(string input, string output) {
    if (input.length() == 0) {
        cout << output << endl;
        return;
    }
    
    // consider single digit as a code
    int digit = input[0] - '0';
    char ch = 'a' + digit - 1;
    printCodes(input.substr(1), output + ch);
    
    // consider two digits as a code
    if (input.length() >= 2) {
        int digits = stoi(input.substr(0, 2));
        if (digits >= 10 && digits <= 26) {
            ch = 'a' + digits - 1;
            printCodes(input.substr(2), output + ch);
        }
    }
}

int main() {
    string input;
    cin >> input;
    printCodes(input, "");
    return 0;
}
 

 


Share this article:

Share on Facebook Share on Twitter Share on LinkedIn Share on WhatsApp