Build lexicographically smallest String from two given Strings
Last Updated :
08 Nov, 2022
Given two strings X and Y of lowercase letters, of length N and M respectively, the task is to build another string Z by performing two types of operations:
- Choose any character from the string X, remove it from X, and add it to the end of Z.
- Choose any character from the string Y, remove it from Y, and add it to the end of Z.
Note: You can only K consecutive operations in a single string. Perform the operations until either X or Y becomes empty.
Examples:
Input: X = “aaaa”, Y =”bbbb”, K = 2
Output: aabaa
Explanation: The smallest lexicographically string possible for K = 2 is “aabaa”.
Select “aa” from X. Then select “b” from Y and “aa” from X again.
Input: X = “ccaaa”, Y =”bbeedd”, K = 3
Output: aaabbcc
Approach: To solve the problem follow the below idea:
We solve this problem by using greedy method.
Sort the strings X and Y and take the characters as follows:
- Keep selecting consecutive characters from the string whose characters are lexicographically smaller till K consecutive characters are selected, or all the characters of that string are selected or the character of the other string becomes lexicographically smaller.
- If K consecutive characters are selected, then do the above operation again on the other string. Repeat these two processes till at least one of the string becomes empty.
Follow the below steps to solve the problem:
- Sort the strings in ascending order.
- Initialize the pointers i, j, p, q to 0.
- Run the loop while any of the strings is non-empty.
- If (X[i] < Y[j] and p < k) or q == k, then,
- Append X[i] to the end of Z and increment i and p by 1 and set q = 0.
- Otherwise, do the following:
- Append Y[j] to the end of Z and increment j and q by 1 and also set p to 0.
- Return the resultant string.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
string buildString(string a, string b, int k)
{
ll n = a.size();
ll m = b.size();
sort(a.begin(), a.end());
sort(b.begin(), b.end());
ll i = 0, j = 0, p = 0, q = 0;
string c = "" ;
while (i < n && j < m) {
if ((a[i] < b[j] && p < k) || q == k) {
c += a[i];
p++;
i++;
q = 0;
}
else {
c += b[j];
q++;
j++;
p = 0;
}
}
return c;
}
int main()
{
string X = "aaaa" ;
string Y = "bbbb" ;
int K = 2;
cout << buildString(X, Y, K) << endl;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
class GFG
{
static String buildstring(String a, String b, int k)
{
int n = a.length();
int m = b.length();
char c1[] = a.toCharArray();
Arrays.sort(c1);
a = new String(c1);
char c2[] = b.toCharArray();
Arrays.sort(c2);
b = new String(c2);
int i = 0 , j = 0 , p = 0 , q = 0 ;
String c = "" ;
while (i < n && j < m)
{
if ((a.charAt(i) < b.charAt(j) && p < k) || q == k)
{
c += a.charAt(i);
p++;
i++;
q = 0 ;
}
else
{
c += b.charAt(j);
q++;
j++;
p = 0 ;
}
}
return c;
}
public static void main (String[] args)
{
String x = "aaaa" ;
String y = "bbbb" ;
int k = 2 ;
System.out.println(buildstring(x,y,k));
}
}
|
Python3
def buildString(a, b, k) :
n = len (a);
m = len (b);
a = list (a)
b = list (b)
a.sort();
b.sort();
i = 0 ; j = 0 ; p = 0 ; q = 0 ;
c = "";
while (i < n and j < m) :
if ((a[i] < b[j] and p < k) or q = = k) :
c + = a[i];
p + = 1 ;
i + = 1 ;
q = 0 ;
else :
c + = b[j];
q + = 1 ;
j + = 1 ;
p = 0 ;
c = "".join(c)
return c;
if __name__ = = "__main__" :
X = "aaaa" ;
Y = "bbbb" ;
K = 2 ;
print (buildString(X, Y, K));
|
C#
using System;
using System.Collections;
public class GFG {
static String buildstring(String a, String b, int k)
{
int n = a.Length;
int m = b.Length;
char [] c1 = a.ToCharArray();
Array.Sort(c1);
a = new String(c1);
char [] c2 = b.ToCharArray();
Array.Sort(c2);
b = new String(c2);
int i = 0, j = 0, p = 0, q = 0;
String c = "" ;
while (i < n && j < m) {
if ((a[i] < b[j] && p < k) || q == k) {
c += a[i];
p++;
i++;
q = 0;
}
else {
c += b[j];
q++;
j++;
p = 0;
}
}
return c;
}
static public void Main()
{
String x = "aaaa" ;
String y = "bbbb" ;
int k = 2;
Console.WriteLine(buildstring(x, y, k));
}
}
|
Javascript
<script>
function buildString( a, b, k)
{
let n = a.length;
let m = b.length;
a.sort;
b.sort;
let i = 0, j = 0, p = 0, q = 0;
let c = "" ;
while (i < n && j < m) {
if ((a[i] < b[j] && p < k) || q == k) {
c += a[i];
p++;
i++;
q = 0;
}
else {
c += b[j];
q++;
j++;
p = 0;
}
}
return c;
}
let X = "aaaa" ;
let Y = "bbbb" ;
let K = 2;
document.write(buildString(X, Y, K));
</script>
|
Time Complexity: O(N*logN +M*logM)
Auxiliary Space: O(N+M)
Share your thoughts in the comments
Please Login to comment...