Distinct strings such that they contains given strings as sub-sequences
Last Updated :
01 Aug, 2022
Given two strings str1 and str2 of lengths M and N respectively. The task is to find all the distinct strings of length M + N such that the frequency of any character in the resultant string is equal to the sum of frequencies of the same character in the given strings and both the given strings are present as a sub-sequence in all the generated strings.
Examples:
Input: str = "aa", str2 = "ab"
Output:
abaa
aaab
aaba
Input: str1 = "ab", str2 = "de"
Output:
deab
daeb
adeb
abde
dabe
adbe
Approach: This problem can be solved using recursion. Set two pointers i and j to the beginnings of strings str1 and str2 respectively. Now, at every recursive call, we have two choices to select either the character at str1[i] or the character at str2[j] and the termination condition will be when the length of the resultant string becomes equal to len(str1) + len(str2). Use an unordered_set in order to avoid duplicates.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Set to store strings
// and avoid duplicates
set<string> 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<string>::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<String> 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 avoid duplicates
stringSet=dict()
# Recursive function to generate the required strings
def find_permutation( str1,str2,len1,len2,i,j,res):
# If current string is 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<String> stringSet = new HashSet<String>();
// 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
JavaScript
<script>
// Javascript implementation of the approach
// Set to store strings
// and avoid duplicates
let stringSet = new Set();
// Recursive function to generate the required strings
function find_permutation(str1,str2,len1,len2,i,j,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
function print_set()
{
for(let s of stringSet.values())
{
document.write(s+"<br>");
}
}
// Driver code
let str1 = "aa", str2 = "ab";
let len1 = str1.length;
let len2 = str2.length;
find_permutation(str1, str2, len1, len2, 0, 0, "");
print_set();
// This code is contributed by unknown2108
</script>
Time complexity: O(2max(length(str1),length(str2)) )
Auxiliary Space: O(length(str1)+length(str2))
Similar Reads
Find the LCS of string such that it does not contains the given string Given three strings s1, s2 and s3. The task is to compute the Longest Common Subsequence of the string s1 and s2, such that it does not contain s3 as a substring. If there is no valid common subsequence, output -1. Examples: Input: s1 = "AJKEQSLOBSROFGZ", s2 = "OVGURWZLWVLUXTH", s3 = "OZ"Output: ORZ
11 min read
Distinct state codes that appear in a string as contiguous sub-strings Every state is represented by string of length 2. For example DL is used for Delhi, HP for Himachal Pradesh, UP for Uttar Pradesh, PB for Punjab etc. Given a string str consisting of uppercase English alphabets only, the task is to find the number of distinct state codes that appear in the string as
4 min read
Different substrings in a string that start and end with given strings Given a string s and two other strings begin and end, find the number of different substrings in the string which begin and end with the given begin and end strings. Examples: Input : s = "geeksforgeeks" begin = "geeks" end = "for" Output : 1 Input : s = "vishakha" begin = "h" end = "a" Output : 2 T
9 min read
Count of Distinct Substrings occurring consecutively in a given String Given a string str, the task is to find the number of distinct substrings that are placed consecutively in the given string. Examples: Input: str = "geeksgeeksforgeeks" Output: 2 Explanation: geeksgeeksforgeeks -> {"geeks"} geeksgeeksforgeeks -> {"e"} Only one consecutive occurrence of "e" is
14 min read
Sub-strings of a string that are prefix of the same string Given a string str, the task is to count all possible sub-strings of the given string that are prefix of the same string. Examples: Input: str = "ababc" Output: 7 All possible sub-string are "a", "ab", "aba", "abab", "ababc", "a" and "ab" Input: str = "abdabc" Output: 8 Approach: Traverse the string
10 min read
Count ways to partition a string such that both parts have equal distinct characters Content has been removed on Author's request.
1 min read