Print all Possible Decodings of a given Digit Sequence | Java 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
import java . io . * ; class GFG { public static boolean nonLower ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! Character . isLowerCase ( s . charAt ( i ) ) ) { return true ; } } return false ; } public static void printCodes ( String output [ ] ) { for ( int i = 0 ; i < output . length ; i ++ ) { if ( nonLower ( output [ i ] ) ) continue ; System . out . println ( output [ i ] ) ; } } public static char getChar ( int n ) { return ( char ) ( n + 96 ) ; } public static String [ ] getCode ( String str ) { if ( str . length ( ) == 0 ) { String ans [ ] = { " " } ; return ans ; } String output1 [ ] = getCode ( str . substring ( 1 ) ) ; String output2 [ ] = new String [ 0 ] ; int firstDigit = ( str . charAt ( 0 ) - '0' ) ; int firstTwoDigit = 0 ; if ( str . length ( ) >= 2 ) { firstTwoDigit = ( str . charAt ( 0 ) - '0' ) * 10 + ( str . charAt ( 1 ) - '0' ) ; if ( firstTwoDigit >= 10 && firstTwoDigit <= 26 ) { output2 = getCode ( str . substring ( 2 ) ) ; } } String output [ ] = new String [ output1 . length + output2 . length ] ; int k = 0 ; for ( int i = 0 ; i < output1 . length ; i ++ ) { char ch = getChar ( firstDigit ) ; output [ i ] = ch + output1 [ i ] ; k ++ ; } for ( int i = 0 ; i < output2 . length ; i ++ ) { char ch = getChar ( firstTwoDigit ) ; output [ k ] = ch + output2 [ i ] ; k ++ ; } return output ; } public static void main ( String [ ] args ) { String input = "101" ; String output [ ] = getCode ( input ) ; printCodes ( output ) ; } }