📌  相关文章
📜  以排序的顺序查找所有可能的字符映射

📅  最后修改于: 2021-04-29 17:08:52             🧑  作者: Mango

给定一个数字,以排序的顺序找到所有可能的字符映射。

例子:

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

方法 :

  1. 创建了一个递归函数,该函数采用一个输入字符数组,其中包含以字符形式存储的数字,一个输出字符数组以及两个可用于在两个数组上进行迭代的变量(例如i和j)。
  2. 使用递归,获得输入数组中i数字的字符,并将具有该数字的对应映射字符存储在输出数组中的j索引处。
  3. 将有两个递归调用。第一次呼叫每次将处理一位数字,而第二次呼叫将一次处理两位数字。
  4. 一次处理两位数字时,该数字应始终小于26,因为组合后,相应的字符必须位于A到Z之间。
  5. 最后,在处理完整个输入字符串的基本情况下,将输出输出数组。

下面是上述方法的实现。

CPP
// C++ program to find all the possible
// string mappings of a given number
// in a sorted order
#include 
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


输出:
ABC
AW
LC