Print all Possible Decodings of a given Digit Sequence | C ++ program for the above approach ; Function to check if all the characters are lowercase or not ; Traverse the string ; If any character is not found to be in lowerCase ; Function to print the decodings ; If all characters are not in lowercase ; Function to return the character corresponding to given integer ; Function to return the decodings ; Base case ; Recursive call ; Stores the characters of two digit numbers ; Extract first digit and first two digits ; Check if it lies in the range of alphabets ; Next recursive call ; Combine both the output in a single final output array ; Index of final output array ; Store the elements of output1 in final output array ; Store the elements of output2 in final output array ; Result the result ; Driver Code ; Function call ; Print function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool nonLower ( string s ) { for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( ! islower ( s [ i ] ) ) { return true ; } } return false ; } void printCodes ( vector < string > output ) { for ( int i = 0 ; i < output . size ( ) ; i ++ ) { if ( nonLower ( output [ i ] ) ) continue ; cout << ( output [ i ] ) << endl ; } } char getChar ( int n ) { return ( char ) ( n + 96 ) ; } vector < string > getCode ( string str ) { if ( str . size ( ) == 0 ) { vector < string > ans ; ans . push_back ( " " ) ; return ans ; } vector < string > output1 = getCode ( str . substr ( 1 ) ) ; vector < string > output2 ( 0 ) ; int firstDigit = ( str [ 0 ] - '0' ) ; int firstTwoDigit = 0 ; if ( str . size ( ) >= 2 ) { firstTwoDigit = ( str [ 0 ] - '0' ) * 10 + ( str [ 1 ] - '0' ) ; if ( firstTwoDigit >= 10 && firstTwoDigit <= 26 ) { output2 = getCode ( str . substr ( 2 ) ) ; } } vector < string > output ( output1 . size ( ) + output2 . size ( ) ) ; int k = 0 ; for ( int i = 0 ; i < output1 . size ( ) ; i ++ ) { char ch = getChar ( firstDigit ) ; output [ i ] = ch + output1 [ i ] ; k ++ ; } for ( int i = 0 ; i < output2 . size ( ) ; i ++ ) { char ch = getChar ( firstTwoDigit ) ; output [ k ] = ch + output2 [ i ] ; k ++ ; } return output ; } int main ( ) { string input = "101" ; vector < string > output = getCode ( input ) ; printCodes ( output ) ; }