给定两个字符串str1和STR2每个长度为N,则任务是生成和打印长度的所有可能的字符串N个这样的,在索引i所生成的字符串的字符是STR1 [I]或STR2 [I]
例子:
Input: str1 = “abc”, str2 = “def”
Output:
abc
abf
aec
aef
dbc
dbf
dec
def
Input: str1 = “a”, str2 = “b”
Output:
a
b
方法:这个问题可以用递归来解决,并在每次递归调用,我们需要选择在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