📌  相关文章
📜  生成所有可能的字符串,以使索引i处的char为str1 [i]或str2 [i]

📅  最后修改于: 2021-04-22 01:27:51             🧑  作者: Mango

给定两个字符串str1STR2每个长度为N,则任务是生成和打印长度的所有可能的字符串N个这样的,在索引i所生成的字符串的字符是STR1 [I]STR2 [I]

例子:

方法:这个问题可以用递归来解决,并在每次递归调用,我们需要选择在STR1字符[I]STR2 [I]字符并追加到结果字符串。终止条件是当结果字符串的长度等于给定字符串的长度时。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Recursive function to generate
// the required strings
void generateStr(char* a, char* b, string s,
                 int count, int len)
{
  
    // If length of the current string is
    // equal to the length of the given
    // strings then the current string
    // is part of the result
    if (count == len) {
        cout << s << endl;
        return;
    }
  
    // Choosing the current character
    // from the string a
    generateStr(a + 1, b + 1, s + (*a),
                count + 1, len);
  
    // Choosing the current character
    // from the string b
    generateStr(a + 1, b + 1, s + (*b),
                count + 1, len);
}
  
// Driver code
int main()
{
    char *a = "abc", *b = "def";
    int n = strlen(a);
  
    // Third argument is an empty
    // string that we will be appended
    // in the recursion calls
    // Fourth arguments is the length of
    // the resultant string so far
    generateStr(a, b, "", 0, n);
  
    return 0;
}


Java
// Java implementation of the approach
  
class GFG 
{
  
    // Recursive function to generate
    // the required strings
    public static void generateStr(String a, String b, 
                                String s, int count, int len)
    {
  
        // If length of the current string is
        // equal to the length of the given
        // strings then the current string
        // is part of the result
        if (count == len) 
        {
            System.out.println(s);
            return;
        }
  
        // Choosing the current character
        // from the string a
        generateStr(a.substring(1), b.substring(1),
                      s + a.charAt(0), count + 1, len);
  
        // Choosing the current character
        // from the string b
        generateStr(a.substring(1), b.substring(1), 
                    s + b.charAt(0), count + 1, len);
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        String a = "abc", b = "def";
        int n = a.length();
  
        // Third argument is an empty
        // string that we will be appended
        // in the recursion calls
        // Fourth arguments is the length of
        // the resultant string so far
        generateStr(a, b, "", 0, n);
  
    }
}
  
// This code is contributed by
// sanjeev2552


Python3
# Python3 implementation of the approach
  
# Recursive function to generate
# the required strings
def generateStr(a, b, s, count, len):
  
    # If length of the current string is
    # equal to the length of the given
    # strings then the current string
    # is part of the result
    if (count == len): 
        print(s);
        return;
  
    # Choosing the current character
    # from the string a
    generateStr(a[1:], b[1:],
                s + a[0], count + 1, len);
  
    # Choosing the current character
    # from the string b
    generateStr(a[1:], b[1:], 
                s + b[0], count + 1, len);
  
# Driver code
a = "abc"; b = "def";
n = len(a);
  
# Third argument is an empty
# string that we will be appended
# in the recursion calls
# Fourth arguments is the length of
# the resultant string so far
generateStr(a, b, "", 0, n);
  
# This code is contributed by Princi Singh


C#
// C# implementation of the approach
using System;
      
class GFG 
{
  
    // Recursive function to generate
    // the required strings
    public static void generateStr(String a, String b, 
                                     String s, int count,
                                               int len)
    {
  
        // If length of the current string is
        // equal to the length of the given
        // strings then the current string
        // is part of the result
        if (count == len) 
        {
            Console.WriteLine(s);
            return;
        }
  
        // Choosing the current character
        // from the string a
        generateStr(a.Substring(1), b.Substring(1),
                    s + a[0], count + 1, len);
  
        // Choosing the current character
        // from the string b
        generateStr(a.Substring(1), b.Substring(1), 
                    s + b[0], count + 1, len);
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        String a = "abc", b = "def";
        int n = a.Length;
  
        // Third argument is an empty
        // string that we will be appended
        // in the recursion calls
        // Fourth arguments is the length of
        // the resultant string so far
        generateStr(a, b, "", 0, n);
    }
}
  
// This code is contributed by Rajput-Ji


输出:
abc
abf
aec
aef
dbc
dbf
dec
def