Lexicographically smallest string possible by merging two sorted strings
Last Updated :
01 Jun, 2021
Given two sorted strings S1 and S2 of lengths N and M respectively, the task is to construct lexicographically the smallest string possible by merging the two given strings and not changing the order of occurrence of characters.
Examples:
Input: S1 = “eefgkors”, S2 = “eegks”
Output: “eeeefggkkorss”
Explanation:
String “eeeefggkkorss” is lexicographically the smallest string that can be formed after merging the two given string S1 and S2.
Input: S1 = “aabcdtx”, S2 = “achilp”
Output: “aaabccdhilptx”
Naive Approach: The simplest approach is to concatenate the given two strings and sort the resultant string to get lexicographically the smallest string.
Time Complexity: O(N + M)*log(N + M))
Auxiliary Space: O(N + M)
Efficient Approach: The above approach can be optimized by using the Two-Pointer technique by comparing characters of the given strings and then, construct the required string accordingly.
Follow the steps below to solve the problem:
- Initialize two pointers, say ptr1 and ptr2, pointing towards the beginning of both the strings S1 and S2 respectively.
- Initialize a string ans = “”, to store the resultant lexicographically the smallest string.
- Iterate until ptr1 is less than N and ptr2 is less than M and perform the following steps:
- If S1[ptr1] is less than the S2[ptr2], then append the character S1[ptr1] to the string ans. Increment ptr1.
- Otherwise, append the character S2[ptr2] to the string ans. Increment ptr2.
- After completing the above steps, one of the pointers doesn’t reach the end of the string.
- Therefore, add all the remaining characters of the string to the end of the string ans.
- Print ans as the resultant string.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void mergeStrings(string s1, string s2)
{
int len1 = s1.size();
int len2 = s2.size();
int pntr1 = 0;
int pntr2 = 0;
string ans = "" ;
while (pntr1 < len1 && pntr2 < len2) {
if (s1[pntr1] < s2[pntr2]) {
ans += s1[pntr1];
pntr1++;
}
else {
ans += s2[pntr2];
pntr2++;
}
}
if (pntr1 < len1) {
ans += s1.substr(pntr1, len1);
}
if (pntr2 < len2) {
ans += s2.substr(pntr2, len2);
}
cout << ans;
}
int main()
{
string S1 = "abdcdtx" ;
string S2 = "achilp" ;
mergeStrings(S1, S2);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void mergeStrings(String s1, String s2)
{
int len1 = s1.length();
int len2 = s2.length();
int pntr1 = 0 ;
int pntr2 = 0 ;
String ans = "" ;
while (pntr1 < len1 && pntr2 < len2)
{
if (s1.charAt(pntr1) < s2.charAt(pntr2))
{
ans += s1.charAt(pntr1);
pntr1++;
}
else
{
ans += s2.charAt(pntr2);
pntr2++;
}
}
if (pntr1 < len1)
{
ans += s1.substring(pntr1, len1);
}
if (pntr2 < len2)
{
ans += s2.substring(pntr2, len2);
}
System.out.println(ans);
}
public static void main (String[] args)
{
String S1 = "abdcdtx" ;
String S2 = "achilp" ;
mergeStrings(S1, S2);
}
}
|
Python3
def mergeStrings(s1, s2):
len1 = len (s1)
len2 = len (s2)
pntr1 = 0
pntr2 = 0
ans = ""
while (pntr1 < len1 and pntr2 < len2):
if (s1[pntr1] < s2[pntr2]):
ans + = s1[pntr1]
pntr1 + = 1
else :
ans + = s2[pntr2]
pntr2 + = 1
if (pntr1 < len1):
ans + = s1[pntr1:pntr1 + len1]
if (pntr2 < len2):
ans + = s2[pntr2:pntr2 + len2]
print (ans)
if __name__ = = '__main__' :
S1 = "abdcdtx"
S2 = "achilp"
mergeStrings(S1, S2)
|
C#
using System;
class GFG
{
static void mergeStrings( string s1, string s2)
{
int len1 = s1.Length;
int len2 = s2.Length;
int pntr1 = 0;
int pntr2 = 0;
string ans = "" ;
while (pntr1 < len1 && pntr2 < len2) {
if (s1[pntr1] < s2[pntr2]) {
ans += s1[pntr1];
pntr1++;
}
else {
ans += s2[pntr2];
pntr2++;
}
}
if (pntr1 < len1) {
ans += s1.Substring(pntr1, len1 - pntr1);
}
if (pntr2 < len2) {
ans += s2.Substring(pntr2, len2 - pntr2);
}
Console.WriteLine(ans);
}
public static void Main()
{
string S1 = "abdcdtx" ;
string S2 = "achilp" ;
mergeStrings(S1, S2);
}
}
|
Javascript
<script>
function mergeStrings( s1, s2)
{
var len1 = s1.length;
var len2 = s2.length;
var pntr1 = 0;
var pntr2 = 0;
var ans = "" ;
while (pntr1 < len1 && pntr2 < len2) {
if (s1[pntr1] < s2[pntr2]) {
ans += s1[pntr1];
pntr1++;
}
else {
ans += s2[pntr2];
pntr2++;
}
}
if (pntr1 < len1) {
ans += s1.substr(pntr1, len1);
}
if (pntr2 < len2) {
ans += s2.substr(pntr2, len2);
}
document.write( ans);
}
var S1 = "abdcdtx" ;
var S2 = "achilp" ;
mergeStrings(S1, S2);
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(N + M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...