Lexicographically largest string possible by repeatedly appending first character of two given strings
Last Updated :
07 Mar, 2022
Given two strings S1 and S2 consisting of N and M lowercase characters, the task is to construct the lexicographically largest string by repeatedly appending the first character from either of the strings and remove that character from the chosen string.
Examples:
Input: S1 = “dbcbb”, S2 = “cdbbb”
Output: “dcdbcbbbbb”
Explanation:
Let ans be the lexicographically largest string which is initially empty and perform the following steps to generate the resultant string:
Take first character from s1: ans = “d”, s1 = “bcbb”, s2 = “cdbbb”
Take first character from s2: ans = “dc”, s1 = “bcbb”, word2 = “dbbb”
Take first character from s2: ans = “dcd”, s1 = “bcbb”, word2 = “bbb”
Take first character from s1: ans = “dcdb”, s1 = “cbb”, word2 = “bbb”
Take first character from s1: ans = “dcbdc”, s1 = “bb”, word2 = “bbb”
Append the remaining 5 b’s from s1 and s2 at the end of ans. Therefore, print “dcdbcbbbbb” as the resultant string.
Input: S1 = “xyzxyz”, S2 = “xywzxyx”
Output: “xyzxyzxywzxyx”
Approach: The given problem can be solved by using the Two-Pointer Approach. Follow the steps below to solve the problem:
- Initialize an empty string, say merge as “” to store the lexicographically largest string.
- Initialize two pointers, say i as 0, j as 0 to traverse both the strings simultaneously.
- Traverse the string until either of the string has been used completely.
- If the substring word1[i, N – 1] is lexicographically greater than or equal to the substring word2[j, M – 1], then append the character word1[i] at the end of the string merge and increment the pointer i by 1.
- Otherwise, append the character word2[i] at the end of the string merge and increment the pointer j by 1.
- After completing the above steps, print the string merge as the resultant string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string largestMerge(string word1,
string word2)
{
string merge = "" ;
while (word1.size() != 0
|| word2.size() != 0) {
if (word1 >= word2) {
merge = merge + word1[0];
word1.erase(word1.begin() + 0);
}
else {
merge = merge + word2[0];
word2.erase(word2.begin() + 0);
}
}
return merge;
}
int main()
{
string S1 = "xyzxyz" ;
string S2 = "xywzxyx" ;
cout << largestMerge(S1, S2);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String largestMerge(String word1,
String word2)
{
String merge = "" ;
while (word1.length() != 0 ||
word2.length() != 0 )
{
if (word1.compareTo(word2) == 0 || ( word1.compareTo(word2) > 0 ))
{
merge = merge + word1.charAt( 0 );
word1 = word1.substring( 1 );
}
else
{
merge = merge + word2.charAt( 0 );
word2 = word2.substring( 1 );
}
}
return merge;
}
public static void main(String[] args)
{
String S1 = "xyzxyz" ;
String S2 = "xywzxyx" ;
System.out.println(largestMerge(S1, S2));
}
}
|
Python3
def largestMerge(word1, word2):
merge = ""
while len (word1) ! = 0 or len (word2) ! = 0 :
if word1 > = word2:
merge = merge + word1[ 0 ]
word1 = word1[ 1 :]
else :
merge = merge + word2[ 0 ]
word2 = word2[ 1 :]
return merge
S1 = "xyzxyz"
S2 = "xywzxyx"
print (largestMerge(S1, S2))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string largestMerge( string word1,
string word2)
{
string merge = "" ;
while (word1.Length != 0 ||
word2.Length != 0)
{
if (String.Compare(word1, word2) == 0 ||
String.Compare(word1, word2) > 0)
{
merge = merge + word1[0];
word1 = word1.Substring(1);
}
else
{
merge = merge + word2[0];
word2 = word2.Substring(1);
}
}
return merge;
}
public static void Main()
{
string S1 = "xyzxyz" ;
string S2 = "xywzxyx" ;
Console.Write(largestMerge(S1, S2));
}
}
|
Javascript
<script>
function largestMerge(word1, word2)
{
let merge = "" ;
while (word1.length != 0 ||
word2.length != 0)
{
if (word1.localeCompare(word2) == 0 || ( word1.localeCompare(word2) > 0))
{
merge = merge + word1[0];
word1 = word1.substring(1);
}
else
{
merge = merge + word2[0];
word2 = word2.substring(1);
}
}
return merge;
}
let S1 = "xyzxyz" ;
let S2 = "xywzxyx" ;
document.write(largestMerge(S1, S2));
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...