
You are given two strings, ‘STR1’ and ‘STR2’. Your task is to construct the lexicographically largest string by merging ‘STR1’ and ‘STR2’. While merging the strings, you need to keep the same order of characters in the merged string as present in ‘STR1’ and ‘STR2’.
For example:
Consider STR1 = ”ca” and STR2 = ”db”. All the possible strings that can be constructed by merging “ca” and “db” are “cadb”, “cdab”, “cdba”, “dcab”, “dcba” and “dbca”, among them the lexicographically largest string is “dcba”. Hence, the answer is “dcba” in this case.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains the string ‘STR1’.
The second line of each test case contains the string ‘STR2’.
Output Format:
For each test case, print a single line containing a single string denoting the lexicographically largest string constructed by merging ‘STR1’ and ‘STR2’.
The output of each test case will be printed in a separate line.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |STR1| <= 3000
1 <= |STR2| <= 3000
Strings 'STR1' and 'STR2' contain lowercase English letters only.
Where 'T' denotes the number of test cases, ‘STR1’ and ‘STR2’ represent the two given strings.
Time limit: 1 sec.
2
ac
b
ca
ab
bac
caba
For the first test case,
All possible strings constructed by merging “ac” and “b” while maintaining the order of characters are “abc”, “acb” and “bac”, among them the lexicographically largest string is “bac”. Hence, the answer is “bac” in this case.
For the second test case,
All possible strings constructed by merging “ca” and “ab” while maintaining the order of characters are “caab”, “caba”, “acab”, “acba” and “abca”, among them the lexicographically largest string is “caba”. Hence, the answer is “caba” in this case.
2
bad
cab
dba
eca
cbadab
edcbaa
Can you think of a recursive approach to find the lexicographically largest string constructed by merging both strings?
The idea is to use a recursive approach to find the lexicographically largest string constructed by merging both strings. We will merge STR1 and STR2 character by character. The recursive idea is clear that there are only two choices, either to choose the next character from the string STR1 or the string STR2 for any character in the merged string. Using this idea, we can construct the lexicographically largest string recursively.
We will define a function largestMergedString(STR1, STR2) to find the lexicographically largest merge of both strings. At each recursive call, we will find the next character to be included in the merged string. The next character should be from the lexicographically larger string between STR1 and STR2. Our approach will be to find the lexicographically greater string and includes its first character in the merged string.
Working of the Recursive function:
O((N + M) * Min(N, M)), where ‘N’ is the length of the string ‘STR1’ and ‘M’ is the length of the string ‘STR2’.
In the worst case, we are doing N + M recursive calls, and in each recursive call, it takes O(Min(N, M)) time to find the lexicographically larger string between STR1 and STR2. Hence, the overall time complexity is O((N + M) * Min(N, M)).
O(N + M), where N is the length of the string ‘STR1’ and M is the length of the string ‘STR2’.
In the worst case, the recursion depth will be N + M. Hence, the overall Space Complexity is O(N + M).