Open In App

Lexicographically largest string possible by repeatedly appending first character of two given strings

Last Updated : 07 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to make the lexicographically
// largest string by merging two strings
string largestMerge(string word1,
                    string word2)
{
    // Stores the resultant string
    string merge = "";
 
    while (word1.size() != 0
           || word2.size() != 0) {
 
        // If the string word1 is
        // lexicographically greater
        // than or equal to word2
        if (word1 >= word2) {
 
            // Update the string merge
            merge = merge + word1[0];
 
            // Erase the first index
            // of the string word1
            word1.erase(word1.begin() + 0);
        }
 
        // Otherwise
        else {
 
            // Update the string merge
            merge = merge + word2[0];
 
            // Erase the first index of
            // the string word2
            word2.erase(word2.begin() + 0);
        }
    }
 
    // Return the final string
    return merge;
}
 
// Driver Code
int main()
{
    string S1 = "xyzxyz";
    string S2 = "xywzxyx";
    cout << largestMerge(S1, S2);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG {
 
// Function to make the lexicographically
// largest string by merging two strings
static String largestMerge(String word1,
                           String word2)
{
     
    // Stores the resultant string
    String merge = "";
 
    while (word1.length() != 0 ||
           word2.length() != 0)
    {
         
        // If the string word1 is
        // lexicographically greater
        // than or equal to word
        if (word1.compareTo(word2) == 0 || ( word1.compareTo(word2) > 0))
        {
 
            // Update the string merge
            merge = merge + word1.charAt(0);
 
            // Erase the first index
            // of the string word1
            word1 = word1.substring(1);
        }
 
        // Otherwise
        else
        {
             
            // Update the string merge
            merge = merge + word2.charAt(0);
 
            // Erase the first index of
            // the string word2
            word2 = word2.substring(1);
        }
    }
 
    // Return the final string
    return merge;
}
 
// Driver Code
public static void main(String[] args)
{
    String S1 = "xyzxyz";
    String S2 = "xywzxyx";
     
    System.out.println(largestMerge(S1, S2));
}
}
 
// This code is contributed by sanjoy_62.


Python3




# Python program for the above approach
 
# Function to make the lexicographically
# largest string by merging two strings
def largestMerge(word1, word2):
   
     # Stores the resultant string
    merge = ""
    while len(word1) != 0 or len(word2) != 0:
       
          # If the string word1 is
        # lexicographically greater
        # than or equal to word2
        if word1 >= word2:
           
          # Update the string merge
            merge = merge + word1[0]
             
             # Erase the first index
            # of the string word1
            word1 = word1[1:]
             
            #  Otherwise
        else:
           
          # Update the string merge
            merge = merge + word2[0]
             
             # Erase the first index
            # of the string word2
            word2 = word2[1:]
             
    # Return the final string       
    return merge
 
# Driver code
S1 = "xyzxyz"
S2 = "xywzxyx"
print(largestMerge(S1, S2))
 
# This code is contributed by Parth Manchanda


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to make the lexicographically
// largest string by merging two strings
static string largestMerge(string word1,
                           string word2)
{
     
    // Stores the resultant string
    string merge = "";
 
    while (word1.Length != 0 ||
           word2.Length != 0)
    {
         
        // If the string word1 is
        // lexicographically greater
        // than or equal to word2
        if (String.Compare(word1, word2) == 0 ||
            String.Compare(word1, word2) > 0)
        {
 
            // Update the string merge
            merge = merge + word1[0];
 
            // Erase the first index
            // of the string word1
            word1 = word1.Substring(1);
        }
 
        // Otherwise
        else
        {
             
            // Update the string merge
            merge = merge + word2[0];
 
            // Erase the first index of
            // the string word2
            word2 = word2.Substring(1);
        }
    }
 
    // Return the final string
    return merge;
}
 
// Driver Code
public static void Main()
{
    string S1 = "xyzxyz";
    string S2 = "xywzxyx";
     
    Console.Write(largestMerge(S1, S2));
 
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript




<script>
// Javascript program for the above approach
 
// Function to make the lexicographically
// largest let by merging two lets
function largestMerge(word1, word2)
{
     
    // Stores the resultant let
    let merge = "";
 
    while (word1.length != 0 ||
           word2.length != 0)
    {
         
        // If the let word1 is
        // lexicographically greater
        // than or equal to word
        if (word1.localeCompare(word2) == 0 || ( word1.localeCompare(word2) > 0))
        {
 
            // Update the let merge
            merge = merge + word1[0];
 
            // Erase the first index
            // of the let word1
            word1 = word1.substring(1);
        }
 
        // Otherwise
        else
        {
             
            // Update the let merge
            merge = merge + word2[0];
 
            // Erase the first index of
            // the let word2
            word2 = word2.substring(1);
        }
    }
 
    // Return the final let
    return merge;
}
 
// Driver Code
 
      let S1 = "xyzxyz";
    let S2 = "xywzxyx";
     
    document.write(largestMerge(S1, S2));
 
// This code is contributed by splevel62.
</script>


Output: 

xyzxyzxywzxyx

 

Time Complexity: O(N*M)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads