Given a number find all the possible mappings of the characters in sorted order.
Examples:
Input: 123 Output: ABC AW LC Explanation: 1 = A; 2 = B; 3 = C; 12 = L; 23 = W {1, 2, 3} / \ / \ "A"{2, 3} "L"{3} / \ / \ / \ / \ "AB"{3} "A"{23} "LC" null / \ / \ / \ / \ "ABC" null "AW" null Input : 2122 Output : BABB BAV BLB UBB UV
Approach :
- A recursive function which takes an input character array which contains the number stored in a form of characters, an output character array, and two variables(say i and j) that can be used to iterate over both the arrays is created.
- Using recursion, character for the ith digit in the input array is obtained and the corresponding mapped character with that digit is stored at the jth index in the output array.
- There will be two recursive calls. The first call will process a single-digit every time, whereas the second call will process two digits at a time.
- While processing two digits at a time, the number should be always less than 26 because after combining, the corresponding character has to lie between A to Z.
- At last, in the base case when the whole input string has been processed, the output array is printed.
Below is the implementation of the above approach.
CPP
// C++ program to find all the possible // string mappings of a given number // in a sorted order #include <bits/stdc++.h> using namespace std; // Function to find the string mappings void mapped( char inputarr[], char outputarr[], int i, int j) { // Base case if (inputarr[i] == '\0' ) { outputarr[j] = '\0' ; cout << outputarr << endl; return ; } // Convert the character to integer int digit = inputarr[i] - '0' ; // To store the characters corresponding // to the digits which are further // stored in outputarr[] char ch = digit + 'A' - 1; outputarr[j] = ch; // First recursive call taking one digit at a time mapped(inputarr, outputarr, i + 1, j + 1); if (inputarr[i + 1] != '\0' ) { int second_digit = inputarr[i + 1] - '0' ; int number = digit * 10 + second_digit; if (number <= 26) { ch = number + 'A' - 1; outputarr[j] = ch; // Second recursive call processing // two digits at a time mapped(inputarr, outputarr, i + 2, j + 1); } } } // Driver code int main() { char inputarr[] = { '1' , '2' , '3' }; int m = pow (2, 3) - 1; int n = sizeof (m) / sizeof ( int ); char outputarr[n]; mapped(inputarr, outputarr, 0, 0); return 0; } |
Java
// Java program to find all the possible // string mappings of a given number // in a sorted order class GFG { // Function to find the string mappings static void mapped( char inputarr[], char outputarr[], int i, int j) { // Base case if (i >= inputarr.length) { String str = new String(outputarr); System.out.println(str.substring( 0 , j)); return ; } // Convert the character to integer int digit = inputarr[i] - '0' ; // To store the characters corresponding // to the digits which are further // stored in outputarr[] char ch = ( char )(digit + ( int )( 'A' ) - 1 ); outputarr[j] = ch; // First recursive call taking one digit at a time mapped(inputarr, outputarr, i + 1 , j + 1 ); if (i + 1 < inputarr.length) { int second_digit = inputarr[i + 1 ] - '0' ; int number = digit * 10 + second_digit; if (number <= 26 ) { ch = ( char )(number + ( int ) 'A' - 1 ); outputarr[j] = ch; // Second recursive call processing // two digits at a time mapped(inputarr, outputarr, i + 2 , j + 1 ); } } } // Driver code public static void main (String[] args) { char inputarr[] = { '1' , '2' , '3' }; int m = ( int )Math.pow( 2 , 3 ) - 1 ; int n = 1 ; char outputarr[] = new char [m]; mapped(inputarr, outputarr, 0 , 0 ); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 program to find all the possible # string mappings of a given number # in a sorted order # Function to find the string mappings def mapped(inputarr, outputarr, i, j): # Base case if (i = = len (inputarr)): print ("".join(outputarr[:j])) return # Convert the character to integer digit = ord (inputarr[i]) - ord ( '0' ) # To store the characters corresponding # to the digits which are further # stored in outputarr[] ch = digit + ord ( 'A' ) - 1 outputarr[j] = chr (ch) # First recursive call taking one digit at a time mapped(inputarr, outputarr, i + 1 , j + 1 ) if (i + 1 < len (inputarr) and [i + 1 ] ! = '\0' ): second_digit = ord (inputarr[i + 1 ]) - ord ( '0' ) number = digit * 10 + second_digit if (number < = 26 ): ch = number + ord ( 'A' ) - 1 outputarr[j] = chr (ch) # Second recursive call processing # two digits at a time mapped(inputarr, outputarr, i + 2 , j + 1 ) # Driver code inputarr = [ '1' , '2' , '3' ] m = pow ( 2 , 3 ) - 1 n = 1 outputarr = [ '0' ] * m mapped(inputarr, outputarr, 0 , 0 ) # This code is contributed by mohit kumar 29 |
C#
// C# program to find all the possible // string mappings of a given number // in a sorted order using System; class GFG { // Function to find the string mappings static void mapped( char []inputarr, char []outputarr, int i, int j) { // Base case if (i >= inputarr.Length) { string str = new string (outputarr); Console.WriteLine(str.Substring(0, j)); return ; } // Convert the character to integer int digit = inputarr[i] - '0' ; // To store the characters corresponding // to the digits which are further // stored in outputarr[] char ch = ( char )(digit + ( int )( 'A' ) - 1); outputarr[j] = ch; // First recursive call taking one digit at a time mapped(inputarr, outputarr, i + 1, j + 1); if (i + 1 < inputarr.Length) { int second_digit = inputarr[i + 1] - '0' ; int number = digit * 10 + second_digit; if (number <= 26) { ch = ( char )(number + ( int ) 'A' - 1); outputarr[j] = ch; // Second recursive call processing // two digits at a time mapped(inputarr, outputarr, i + 2, j + 1); } } } // Driver code public static void Main () { char []inputarr = { '1' , '2' , '3' }; int m = ( int )Math.Pow(2, 3) - 1; int n = 1; char []outputarr = new char [m]; mapped(inputarr, outputarr, 0, 0); } } // This code is contributed by AnkitRai01 |
Javascript
<script> // JavaScript program to find all the possible // string mappings of a given number // in a sorted order // Function to find the string mappings function mapped(inputarr,outputarr,i,j) { // Base case if (i >= inputarr.length) { let str = (outputarr).join( "" ); document.write(str.substring(0, j)+ "<br>" ); return ; } // Convert the character to integer let digit = inputarr[i].charCodeAt(0) - '0' .charCodeAt(0); // To store the characters corresponding // to the digits which are further // stored in outputarr[] let ch = String.fromCharCode(digit + ( 'A' ).charCodeAt(0) - 1); outputarr[j] = ch; // First recursive call taking one digit at a time mapped(inputarr, outputarr, i + 1, j + 1); if (i + 1 < inputarr.length) { let second_digit = inputarr[i + 1].charCodeAt(0) - '0' .charCodeAt(0); let number = digit * 10 + second_digit; if (number <= 26) { ch = String.fromCharCode(number + 'A' .charCodeAt(0) - 1); outputarr[j] = ch; // Second recursive call processing // two digits at a time mapped(inputarr, outputarr, i + 2, j + 1); } } } // Driver code let inputarr=[ '1' , '2' , '3' ]; let m = Math.pow(2, 3) - 1; let n = 1; let outputarr = new Array(m); mapped(inputarr, outputarr, 0, 0); // This code is contributed by patel2127 </script> |
ABC AW LC
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!