给定两个分别为长度M和N的字符串str1和str2 。任务是找到所有长度为M + N的不同字符串,以使结果字符串中任何字符的频率等于给定字符串相同字符的频率之和,并且两个给定字符串都作为子元素出现所有生成的字符串的-sequence。
例子:
Input: str = “aa”, str2 = “ab”
Output:
abaa
aaab
aaba
Input: str1 = “ab”, str2 = “de”
Output:
deab
daeb
adeb
abde
dabe
adbe
方法:可以使用递归解决此问题。将两个指针i和j分别设置为字符串str1和str2的开头。现在,在每一个递归调用我们有两个选择以选择在STR1的字符[Ⅰ]或在STR2 [j]的字符和终止条件将是当得到的字符串的长度变得等于LEN(STR1)+ LEN (str2) 。使用unordered_set以避免重复。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Set to store strings
// and avoid duplicates
set stringSet;
// Recursive function to generate the required strings
void find_permutation(string& str1, string& str2, int len1,
int len2, int i, int j, string res)
{
// If current string is part of the result
if (res.length() == len1 + len2) {
// Insert it into the set
stringSet.insert(res);
return;
}
// If character from str1 can be chosen
if (i < len1)
find_permutation(str1, str2, len1, len2,
i + 1, j, res + str1[i]);
// If character from str2 can be chosen
if (j < len2)
find_permutation(str1, str2, len1, len2,
i, j + 1, res + str2[j]);
}
// Function to print the generated
// strings from the set
void print_set()
{
set::iterator itr;
for (itr = stringSet.begin(); itr != stringSet.end(); itr++)
cout << (*itr) << endl;
}
// Driver code
int main()
{
string str1 = "aa", str2 = "ab";
int len1 = str1.length();
int len2 = str2.length();
find_permutation(str1, str2, len1,
len2, 0, 0, "");
print_set();
return 0;
}
Java
// Java implementation of the approach
import java.util.HashSet;
class GFG
{
// Set to store strings
// and avoid duplicates
static HashSet stringSet = new HashSet<>();
// Recursive function to generate the required strings
public static void find_permutation(String str1, String str2,
int len1, int len2, int i,
int j, String res)
{
// If current string is part of the result
if (res.length() == len1 + len2)
{
// Insert it into the set
stringSet.add(res);
return;
}
// If character from str1 can be chosen
if (i < len1)
find_permutation(str1, str2, len1, len2, i + 1,
j, res + str1.charAt(i));
// If character from str2 can be chosen
if (j < len2)
find_permutation(str1, str2, len1, len2, i, j + 1,
res + str2.charAt(j));
}
// Function to print the generated
// strings from the set
public static void print_set()
{
for (String s : stringSet)
System.out.println(s);
}
// Driver code
public static void main(String[] args)
{
String str1 = "aa", str2 = "ab";
int len1 = str1.length();
int len2 = str2.length();
find_permutation(str1, str2, len1, len2, 0, 0, "");
print_set();
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 implementation of the approach
# Set to store strings
# and aduplicates
stringSet=dict()
# Recursive function to generate the required strings
def find_permutation( str1,str2,len1,len2,i,j,res):
# If currentis part of the result
if (len(res) == len1 + len2):
# Insert it into the set
stringSet[res]=1
return
# If character from str1 can be chosen
if (i < len1):
find_permutation(str1, str2, len1, len2,i + 1, j, res + str1[i])
# If character from str2 can be chosen
if (j < len2):
find_permutation(str1, str2, len1, len2,i, j + 1, res + str2[j])
# Function to print the generated
# strings from the set
def print_set():
for i in stringSet:
print(i)
# Driver code
str1 = "aa"
str2 = "ab"
len1 = len(str1)
len2 = len(str2)
find_permutation(str1, str2, len1,len2, 0, 0, "")
print_set()
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Set to store strings
// and avoid duplicates
static HashSet stringSet = new HashSet();
// Recursive function to generate the required strings
public static void find_permutation(String str1, String str2,
int len1, int len2, int i,
int j, String res)
{
// If current string is part of the result
if (res.Length == len1 + len2)
{
// Insert it into the set
stringSet.Add(res);
return;
}
// If character from str1 can be chosen
if (i < len1)
find_permutation(str1, str2, len1, len2,
i + 1, j, res + str1[i]);
// If character from str2 can be chosen
if (j < len2)
find_permutation(str1, str2, len1, len2,
i, j + 1, res + str2[j]);
}
// Function to print the generated
// strings from the set
public static void print_set()
{
foreach (String s in stringSet)
Console.WriteLine(s);
}
// Driver code
public static void Main(String[] args)
{
String str1 = "aa", str2 = "ab";
int len1 = str1.Length;
int len2 = str2.Length;
find_permutation(str1, str2,
len1, len2, 0, 0, "");
print_set();
}
}
// This code is contributed by 29AjayKumar
输出:
aaab
aaba
abaa