📌  相关文章
📜  打印给定数字序列的所有可能的解码

📅  最后修改于: 2021-04-22 09:44:53             🧑  作者: Mango

给定数字字符串str ,其中1代表‘a’ ,2代表‘b’ ,…,26代表‘z’ ,任务是打印所有可以从str获得的字母字符串。

例子:

方法:可以观察到,每个单个字符代表一个除0之外的字母。此问题是递归的,可以分解为子问题。终止条件为传递的字符串为空时。以下是解决问题的步骤:

  1. 创建一个辅助函数getChar() ,该函数返回给定数字字符的相应字母。
  2. 创建一个递归函数,该函数将输入作为字符串,并返回提取的每个字符的所需答案的数组。
  3. 基本情况是输入字符串为空时。在这种情况下,返回一个长度为1的数组,其中包含一个空字符串。
  4. 使用helper函数提取每个单个字符,然后将其附加到空字符串,然后将其存储在一个数组中,例如output1
  5. 同时检查字符长度是否大于或等于两个,还检查提取的两个字符是否在字母范围内。现在,将相应的字符存储在一个数组中,例如output2
  6. 然后创建一个最终的输出数组,其长度将是output1output2的长度之和,并存储它们的答案并返回它。
  7. 对提取的每一个或一对相邻字符重复上述步骤。现在,返回获得的最终数组。
  8. 遍历数组并为每个字符串生成对应的小写字母字符串并打印出来。

下面是上述方法的实现:

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


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to check if all the
    // characters are lowercase or not
    public static boolean
    nonLower(String s)
    {
        // Traverse the string
        for (int i = 0; i < s.length(); i++) {
 
            // If any character is not
            // found to be in lowerCase
            if (!Character
                     .isLowerCase(s.charAt(i))) {
                return true;
            }
        }
 
        return false;
    }
 
    // Function to print the decodings
    public static void
    printCodes(String output[])
    {
        for (int i = 0; i < output.length; i++) {
 
            // If all characters are not
            // in lowercase
            if (nonLower(output[i]))
                continue;
            System.out.println(output[i]);
        }
    }
 
    // Fuction to return the character
    // corresponding to given integer
    public static char getChar(int n)
    {
        return (char)(n + 96);
    }
 
    // Function to return the decodings
    public static String[] getCode(
        String str)
    {
        // Base case
        if (str.length() == 0) {
 
            String ans[] = { "" };
            return ans;
        }
 
        // Recursive call
        String output1[]
            = getCode(str.substring(1));
 
        // Stores the characters of
        // two digit numbers
        String output2[] = new String[0];
 
        // Extract first digit and
        // first two digits
        int firstDigit
            = (str.charAt(0) - '0');
        int firstTwoDigit = 0;
 
        if (str.length() >= 2) {
 
            firstTwoDigit
                = (str.charAt(0) - '0') * 10
                  + (str.charAt(1) - '0');
 
            // Check if it lies in the
            // range of alphabets
            if (firstTwoDigit >= 10
                && firstTwoDigit <= 26) {
 
                // Next recursive call
                output2
                    = getCode(str.substring(2));
            }
        }
 
        // Combine both the output in a
        // single final output array
        String output[]
            = new String[output1.length
                         + output2.length];
 
        // Index of final output array
        int k = 0;
 
        // Store the elements of output1
        // in final output array
        for (int i = 0; i < output1.length; i++) {
 
            char ch = getChar(firstDigit);
 
            output[i] = ch + output1[i];
            k++;
        }
 
        // Store the elements of output2
        // in final output array
        for (int i = 0; i < output2.length; i++) {
 
            char ch = getChar(firstTwoDigit);
 
            output[k] = ch + output2[i];
            k++;
        }
 
        // Result the result
        return output;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String input = "101";
 
        // Function call
        String output[] = getCode(input);
 
        // Print function call
        printCodes(output);
    }
}


Python3
# Python3 program for
# the above approach
  
# Function to check if all the
# characters are lowercase or not
def nonLower(s):
     
    # Traverse the string
    for i in range(len(s)):
         
        # If any character is not
        # found to be in lowerCase
        if not s[i].islower():
            return True
             
    return False
 
# Function to print the decodings
def printCodes(output):
 
    for i in range(len(output)):
   
        # If all characters are not
        # in lowercase
        if (nonLower(output[i])):
            continue
   
        print(output[i])
   
# Fuction to return the character
# corresponding to given integer
def getChar(n):
 
  return chr(n + 96)
 
# Function to return the decodings
def getCode(str):
     
    # Base case
    if (len(str) == 0):
        ans = [""]
        return ans
         
    # Recursive call
    output1 = getCode(str[1:])
     
    # Stores the characters of
    # two digit numbers
    output2 = []
     
    # Extract first digit and
    # first two digits
    firstDigit = (ord(str[0]) - ord('0'))
    firstTwoDigit = 0
     
    if (len(str) >= 2):
      firstTwoDigit = ((ord(str[0]) - ord('0')) * 10 +
                       (ord(str[1]) - ord('0')))
     
      # Check if it lies in the
      # range of alphabets
      if (firstTwoDigit >= 10 and firstTwoDigit <= 26):
       
        # Next recursive call
        output2 = getCode(str[2:])
     
    # Combine both the output in a
    # single readonly output array
    output = ['' for i in range(len(output1) +
                                len(output2))]
     
    # Index of readonly output array
    k = 0
     
    # Store the elements of output1
    # in readonly output array
    for i in range(len(output1)):
        ch = getChar(firstDigit)
        output[i] = ch + output1[i]
        k += 1
         
    # Store the elements of output2
    # in readonly output array
    for i in range(len(output2)):
        ch = getChar(firstTwoDigit)
        output[k] = ch + output2[i]
        k += 1
     
    # Result the result
    return output
     
# Driver Code
if __name__=='__main__':
     
    input = "101"
     
    # Function call
    output = getCode(input)
  
    # Print function call
    printCodes(output)
 
# This code is contributed by rutvik_56


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to check if all the
// characters are lowercase or not
public static bool nonLower(String s)
{
  // Traverse the string
  for (int i = 0; i < s.Length; i++)
  {
    // If any character is not
    // found to be in lowerCase
    if (!char.IsLower(s[i]))
    {
      return true;
    }
  }
  return false;
}
 
// Function to print the decodings
public static void printCodes(String []output)
{
  for (int i = 0; i < output.Length; i++)
  {
    // If all characters are not
    // in lowercase
    if (nonLower(output[i]))
      continue;
    Console.WriteLine(output[i]);
  }
}
 
// Fuction to return the character
// corresponding to given integer
public static char getChar(int n)
{
  return (char)(n + 96);
}
 
// Function to return the decodings
public static String[] getCode(String str)
{
  // Base case
  if (str.Length == 0)
  {
    String []ans = { "" };
    return ans;
  }
 
  // Recursive call
  String []output1 = getCode(str.Substring(1));
 
  // Stores the characters of
  // two digit numbers
  String []output2 = new String[0];
 
  // Extract first digit and
  // first two digits
  int firstDigit = (str[0] - '0');
  int firstTwoDigit = 0;
 
  if (str.Length >= 2)
  {
    firstTwoDigit = (str[0] - '0') * 10 +
                    (str[1] - '0');
 
    // Check if it lies in the
    // range of alphabets
    if (firstTwoDigit >= 10 &&
        firstTwoDigit <= 26)
    {
      // Next recursive call
      output2 = getCode(str.Substring(2));
    }
  }
   
  // Combine both the output in a
  // single readonly output array
  String []output = new String[output1.Length +
                               output2.Length];
 
  // Index of readonly output array
  int k = 0;
 
  // Store the elements of output1
  // in readonly output array
  for (int i = 0; i < output1.Length; i++)
  {
    char ch = getChar(firstDigit);
    output[i] = ch + output1[i];
    k++;
  }
 
  // Store the elements of output2
  // in readonly output array
  for (int i = 0; i < output2.Length; i++)
  {
    char ch = getChar(firstTwoDigit);   
    output[k] = ch + output2[i];
    k++;
  }
 
  // Result the result
  return output;
}
 
// Driver Code
public static void Main(String[] args)
{
  String input = "101";
 
  // Function call
  String []output = getCode(input);
 
  // Print function call
  printCodes(output);
}
}
 
// This code is contributed by Rajput-Ji


输出:
ja

时间复杂度: O(2 N )
空间复杂度: O(N)