Lexicographically largest possible by merging two strings by adding one character at a time
Given two strings S and T, the task is to merge these two strings by adding one character at a time from the beginning of either string to form a resultant string. The resultant string should be lexicographically the largest string that can be formed by merging the strings S and T.
Examples:
Input: S = “dbcbb”, T = “cdbbb”
Output : dcdbcbbbbb
Input : S = “geeks“, T = “forgeeks”
Output : gforgeekseeks
Approach: The simplest idea to solve the problem is to greedily select the first characters from the string which is lexicographically bigger than others. Therefore, Greedy algorithm and Recursion can be used to solve the problem. Follow the steps below to solve the problem:
- If either of the string lengths is 0, then return S + T as the answer.
- If S is lexicographically larger than T, then return S[0] + largestMerge(S.substr(1), T).
- Otherwise, take the first character of T and call the recursive function largestMerge(S, T.substr(1)).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string largestMerge(string s1, string s2)
{
if (s1.size() == 0 || s2.size() == 0)
return s1 + s2;
if (s1 > s2) {
return s1[0] + largestMerge(s1.substr(1), s2);
}
return s2[0] + largestMerge(s1, s2.substr(1));
}
int main()
{
string s1 = "geeks" ;
string s2 = "forgeeks" ;
cout << largestMerge(s1, s2) << endl;
return 0;
}
|
Java
public class Main
{
static String largestMerge(String s1, String s2)
{
if (s1.length() == 0 || s2.length() == 0 )
return s1 + s2;
if (s1.compareTo(s2) > 0 ) {
return s1.charAt( 0 )
+ largestMerge(s1.substring( 1 ), s2);
}
return s2.charAt( 0 ) + largestMerge(s1, s2.substring( 1 ));
}
public static void main(String[] args)
{
String s1 = "geeks" ;
String s2 = "forgeeks" ;
System.out.print(largestMerge(s1, s2));
}
}
|
Python3
def largestMerge(s1, s2):
if len (s1) = = 0 or len (s2) = = 0 :
return s1 + s2
if (s1 > s2):
return s1[ 0 ] + largestMerge(s1[ 1 :], s2)
return s2[ 0 ] + largestMerge(s1, s2[ 1 :])
if __name__ = = '__main__' :
s1 = "geeks"
s2 = "forgeeks"
print (largestMerge(s1, s2))
|
C#
using System;
class GFG {
static string largestMerge( string s1, string s2)
{
if (s1.Length == 0 || s2.Length == 0)
return s1 + s2;
if ( string .Compare(s1, s2) == 1) {
return s1[0]
+ largestMerge(s1.Substring(1), s2);
}
return s2[0] + largestMerge(s1, s2.Substring(1));
}
public static void Main()
{
string s1 = "geeks" ;
string s2 = "forgeeks" ;
Console.Write(largestMerge(s1, s2));
}
}
|
Javascript
<script>
const largestMerge = (s1, s2) =>
{
if (s1.length == 0 || s2.length == 0)
return s1 + s2;
if (s1 > s2) {
return s1[0] + largestMerge(s1.substr(1), s2);
}
return s2[0] + largestMerge(s1, s2.substr(1));
}
s1 = "geeks" ;
s2 = "forgeeks" ;
document.write(largestMerge(s1, s2));
</script>
|
Time Complexity: O(M×N), where M and N are the length of string s1 and s2 respectively.
Auxiliary Space: O(1)
Last Updated :
05 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...