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


Lexicographic ordering refers to a method of comparing sequences of elements, akin to the ordering of words in a dictionary. The comparison process involves evaluating the first element of each sequence. If the first element of the first sequence is considered smaller than the first element of the second sequence, the first sequence is considered lexicographically less than the second sequence. Conversely, if the first element of the first sequence is deemed larger than the first element of the second sequence, the first sequence is lexicographically greater than the second sequence.

Problem Statement

The objective is to generate the lexicographically largest string by iteratively appending the initial character from either string s1 or s2 and subsequently removing that character from the chosen string.

Example

Input

s1 = “abcabc”, s2 = “abdcaba”

Output

“abcabcabdcaba”

Explanation

Step s1 s2 Answer
1 “abcabc” “abdcaba” “”
2 “bcabc” “abdcaba” “a”
3 “cabc” “abdcaba” “ab”
4 “abc” “abdcaba” “abc”
5 “bc” “abdcaba” “abca”
6 “c” “abdcaba” “abcab”
7 “” “abdcaba” “abcabc”
8 “” “” “abcabcabdcaba”

Solution Approach

An intuitive approach is to repeatedly compare the first characters of the two given strings. The larger character is then appended to the answer string and the smaller character is removed from the corresponding string. This process is repeated until one of the strings is empty. The remaining characters of the strings are then appended to the answer string.

The following is a step-by-step explanation of how the algorithm works:

  • Initialise the answer string to an empty string.

  • While both strings are not empty:

    • Compare the first characters of the strings.

    • Add the greater character to the resultant string by appending it.

    • Remove the larger character from the string.

  • Add the remaining characters from the strings to the resultant string by appending them.

  • Return the answer string.

Algorithm

Function constructLargestString()

  • Initialise string ans = “”

  • while (!empty(s1) and !empty(s2))

if(s1[0] >= s2[0])
ans += s1[0]
s1.erase(s1.begin())
		else
			ans += s2[0]
			s2.erase(s2.begin())
  • ans += s1 + s2

  • return ans

Function main()

  • Declare string s1 and s2

  • Initialise s1 and s2

  • Function call constructLargestString()

  • Print ans

Example: C++ Program

The following C++ program code constructs the lexicographically largest string from two given strings s1 and s2 using the constructLargestString() function.The function accepts two strings as input parameters and constructs the lexicographically largest string by comparing the initial character of both strings. It appends the larger character to the answer string while removing it from the original string using the string.erase() function. This process is repeated until all characters have been appended to the answer string.This continues while both the strings are non-empty. Once either or both the strings are empty, all the remaining characters are concatenated to the answer string.

Example

// C++ program to construct the lexicographically largest string from two given strings.
#include <bits/stdc++.h>
using namespace std;
// The purpose of this function is to generate the lexicographically largest string by utilising two given strings.
string constructLargestString(string s1, string s2){
   string ans = "";
   // While both strings are not empty,
   while (s1.size() > 0 && s2.size() > 0){
   // Compare the first characters of the strings. Add the larger character to the resultant string by appending it. Eliminate the larger character from the string.
   if (s1[0] >= s2[0]) {
   ans += s1[0];
   s1.erase(s1.begin());
   }
   else{
   ans += s2[0];
   s2.erase(s2.begin());
   }
   }   
// Add the remaining characters from the strings to the resultant string by appending them.
   ans += s1 + s2;
   return ans;
}
// main function
int main(){
   string s1, s2;
   s1 = "abcabc", s2 = "abdcaba";
   // Print the lexicographically largest string.
   cout<< constructLargestString(s1, s2) << endl;
   return 0;
}

Output

abcabcabdcaba

Time and Space Complexity Analysis

Time complexity: O(min(N, M))

  • The provided code employs a while loop to compare the characters of the two input strings until one of them becomes empty. Each iteration of the loop examines the initial characters of the strings and executes certain operations. The total number of iterations is determined by the length of the shorter string between s1 and s2.

  • In each iteration, the code performs constant-time operations such as appending a character to the answer string, removing a character from a string, and comparing characters.

  • Once the loop concludes, any remaining characters from the non-empty string are added to the resultant string.

  • As a result, the time complexity of the code is O(min(N, M)), where N and M represent the lengths of the input strings s1 and s2, respectively. This complexity is linear with respect to the length of the shorter string.

Space Complexity: O(N + M)

  • The code initialises a string variable called "ans" to hold the lexicographically largest string that is constructed.

  • Besides the input strings s1 and s2, the code does not utilise any additional data structures that increase in size with the input.

  • Therefore, the space complexity of the code is O(N + M), where N and M represent the lengths of the input strings s1 and s2, respectively.

Conclusion

In this article we discussed an approach to construct the lexicographically largest string from two given strings. We discussed the problem statement with the help of a detailed example with step by step explanation. The solution approach provided is fairly intuitive and includes the algorithm, C++ program code, and the time and space complexity analysis.

Updated on: 27-Aug-2023

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements