Print strings that can be formed with two strings

Amarnath

I am trying to solve the below problem. But could not come up with a correct solution

Given two strings say xyz and abc. Find all the strings that can be formed with these two strings. One constraint is that the sequence of characters in each string should remain the same.

Examples:

xyabzc - Valid

xabcyz - Valid

abcxyz - Valid

xyzacb - Invalid. (b cannot come before c)

I tried the following,

Concatenated both the strings into a new string. Got all the permutations of the string and removed the one's that are not following the above constraint. I know this is very vague but that's the only solution I came up.

Please suggest a better approach.

Vedang Mehta

You can solve this problem very easily with recursion. In each step, you can either add a character from the first string or a character from the second string.

C++ code -

#include <iostream>
#include <string>
#include <vector>

using namespace std;

string s1, s2;  //Input strings
vector <string> v;  //To store resultant strings

void merge_strings(int i,int j,string res)
{
        if(i == s1.length() && j == s2.length())
                v.push_back(res);
        if(i < s1.length())
                merge_strings(i + 1,j,res + s1[i]);
        if(j < s2.length())
                merge_strings(i,j + 1,res + s2[j]);
}

int main()
{
        s1 = "abc";
        s2 = "xyz";
        merge_strings(0,0,"");
        for(string s : v)
                cout << s << endl;
        return 0;
}

Output -

abcxyz
abxcyz
abxycz
abxyzc
axbcyz
axbycz
axbyzc
axybcz
axybzc
axyzbc
xabcyz
xabycz
xabyzc
xaybcz
xaybzc
xayzbc
xyabcz
xyabzc
xyazbc
xyzabc

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I put two strings in one print function?

Concat two strings, print and iterate

all strings of length k that can be formed from a set of n characters

Can printf print defined strings?

How to print two dimensional array of strings as String

Bash print file contents between two strings

Print all possible strings of length k formed from set of n characters without repetitive structure

How can I add strings and print them?

Laravel strings Foreign Key incorrectly formed

Difference between char[][] and char**, why puts(strings[0]) can not print(strings is defined as char**strings)

How can I compare two shuffled strings?

Can concating two strings show resulting string

How can I compare two strings in NUnitLite?

Can´t compare two strings in python

How can I split strings by two commas ,,?

Two strings print as same but don't pass equality test?

Compare two String[] arrays and print out the strings which differ

How to print a two dimensional array of strings(with spaces) in C?

How to print message in terminal with concat two strings in flutter?

python difflib print only matches part between two strings

Concat two strings and print it on a label c# wpf

if two strings are in multiple lines print them only once

Print two ArrayLists side by side when one ArrayList is for Strings

How to compare two strings by character and print matching positions in python

Python: Print selected string in between two strings that call it

How can I compare two strings and try to print out comman latters but i could not avoid to repeat a latter more than once

Print Combining Strings and Numbers

Print Strings in Python (TestDome)

How to print an array of strings