📌  相关文章
📜  通过包括给定的字符串的字符的字符或ASCII值产生的子序列

📅  最后修改于: 2021-09-06 06:38:19             🧑  作者: Mango

给定的长度为N的字符串str,任务是打印给定的字符串的所有可能的非空的子序列,使得所述子序列或者包含字符或从给定字符串中的字符的ASCII值。

例子:

处理方法:按照以下步骤解决问题:

  • 使用变量i迭代给定字符串的每个字符以生成字符串的所有可能子序列。
  • 对于每个i字符,可以执行以下三个操作:
    • 在子序列中包含str 的i字符。
    • 不要在子序列中包含str 的i字符。
    • 在子序列中包含str 的i字符的 ASCII 值。
  • 因此,求解该问题的递推关系如下:
  • 使用上面的递推关系,根据给定的条件打印所有可能的子序列。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
// Function to print subsequences containing
// ASCII value of the characters or the
// the characters of the given string
void FindSub(string str, string res,
             int i)
{
     
    // Base Case
    if (i == str.length())
    {
         
        // If length of the
        // subsequence exceeds 0
        if (res.length() > 0)
        {
             
            // Print the subsequence
           cout << res << " ";
        }
        return;
    }
 
    // Stores character present at
    // i-th index of str
    char ch = str[i];
 
    // If the i-th character is not
    // included in the subsequence
    FindSub(str, res, i + 1);
 
    // Including the i-th characer
    // in the subsequence
    FindSub(str, res + ch, i + 1);
 
    // Include the ASCII value of the
    // ith character in the subsequence
    FindSub(str, res + to_string(int(ch)), i + 1);
}
 
// Driver Code
int main()
{
    string str = "ab";
    string res = "";
     
    // Stores length of str
    int N = str.length();
 
    FindSub(str, res, 0);
}
 
// This code is contributed by ipg2016107


Java
// Java program to implement
// the above approach
class GFG {
 
    // Function to print subsequences containing
    // ASCII value of the characters or the
    // the characters of the given string
    static void FindSub(String str, String res,
                        int i)
    {
        // Base Case
        if (i == str.length()) {
 
            // If length of the
            // subsequence exceeds 0
            if (res.length() > 0) {
 
                // Print the subsequence
                System.out.print(res + " ");
            }
            return;
        }
 
        // Stores character present at
        // i-th index of str
        char ch = str.charAt(i);
 
        // If the i-th character is not
        // included in the subsequence
        FindSub(str, res, i + 1);
 
        // Including the i-th characer
        // in the subsequence
        FindSub(str, res + ch, i + 1);
 
        // Include the ASCII value of the
        // ith character in the subsequence
        FindSub(str, res + (int)ch, i + 1);
        ;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        String str = "ab";
 
        String res = "";
 
        // Stores length of str
        int N = str.length();
 
        FindSub(str, res, 0);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to print subsequences containing
# ASCII value of the characters or the
# the characters of the given string
def FindSub(string , res, i) :
     
    # Base Case
    if (i == len(string)):
 
        # If length of the
        # subsequence exceeds 0
        if (len(res) > 0) :
             
            # Print the subsequence
            print(res, end=" ");
        
        return;
 
    # Stores character present at
    # i-th index of str
    ch = string[i];
 
    # If the i-th character is not
    # included in the subsequence
    FindSub(string, res, i + 1);
 
    # Including the i-th characer
    # in the subsequence
    FindSub(string, res + ch, i + 1);
 
    # Include the ASCII value of the
    # ith character in the subsequence
    FindSub(string, res + str(ord(ch)), i + 1);
 
# Driver Code
if __name__ == "__main__" :
     
    string = "ab";
 
    res = "";
 
    # Stores length of str
    N = len(string);
 
    FindSub(string, res, 0);
   
# This code is contributed by AnkitRai01


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to print subsequences containing
// ASCII value of the characters or the
// the characters of the given string
static void FindSub(string str, string res,
                    int i)
{
     
    // Base Case
    if (i == str.Length)
    {
         
        // If length of the
        // subsequence exceeds 0
        if (res.Length > 0)
        {
             
            // Print the subsequence
            Console.Write(res + " ");
        }
        return;
    }
 
    // Stores character present at
    // i-th index of str
    char ch = str[i];
 
    // If the i-th character is not
    // included in the subsequence
    FindSub(str, res, i + 1);
 
    // Including the i-th characer
    // in the subsequence
    FindSub(str, res + ch, i + 1);
 
    // Include the ASCII value of the
    // ith character in the subsequence
    FindSub(str, res + (int)ch, i + 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    string str = "ab";
    string res = "";
 
    // Stores length of str
    int N = str.Length;
 
    FindSub(str, res, 0);
}
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
b 98 a ab a98 97 97b 9798

时间复杂度: O(3 N )
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live