Lexicographically smallest string which differs from given strings at exactly K indices
Last Updated :
24 Nov, 2022
Given two strings S1 and S2 of length N and a positive integer K, the task is to find the lexicographically smallest string such that it differs from the given two strings S1 and S2 at exactly K places. If there is no such string exists then print “-1”.
Examples:
Input: N = 4, K = 3, S1 = “ccbb”, S2 = “caab”
Output: abcb
Explanation:
String “abcb” differs from S1 at exactly 3 places i.e., at positions 1, 2 and 3, and
String “abcb” differs from S2 at exactly 3 places i.e., at positions 1, 2 and 3.
Input: N = 5, K = 1, S1 = “cbabb”, S2 = “babaa”
Output: -1
Explanation:
No such string exists that simultaneously differs from S1 and S2 only at 1 position.
Approach:
- Instead of building S3 from scratch, pick S2 as the resultant string S3 and try to modify it according to the given constraints.
- Find at how many positions the answer string S3 differs from S1.
- Let them differ from each other at exactly d positions. Then the minimum number of places in which the answer string S3 can differ from both the string is ceil(d/2) and maximum number of places can be N.
- If K is within the range [ceil(d/2), N] then S3 exists otherwise S3 doesn’t exists and print “-1”.
- For string S3 below are the cases:
- K greater than d
- If K is greater than d then modify all the positions at which S1 differs from S3 such that after modification, S3 at that position will now differ from both S1 and S2. Decrement K.
- Modify the positions at which S1 is the same as S3 such that after modification, S3 at that position will now differ from both S1 and S2. Decrement K.
- K less than or equal to d
- In this case, only modify the S3 position at which S1 and S3 differ.
- After modification let X be the number of positions at which only S1 differs from S3 and S2 differs from S3.
- Let T be the number of positions which both S1 and S2 differ from S3. Then the equation’s will be:
(2 * X) + T = d
X + T = K
- Solving these equations, the values of X and T can be obtained, and modify the answer string S3 accordingly.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
char arr[] = { 'a' , 'b' , 'c' };
void findString( int n, int k,
string s1, string s2)
{
string s3 = s2;
int d = 0;
for ( int i = 0;
i < s1.size(); i++) {
if (s1[i] != s2[i])
d++;
}
if ((d + 1) / 2 > k) {
cout << "-1" << endl;
return ;
}
else {
if (k <= d) {
int X = d - k;
int T = 2 * k - d;
for ( int i = 0;
i < s3.size(); i++) {
if (s1[i] != s2[i]) {
if (T > 0) {
for ( int j = 0;
j < 3; j++) {
if (arr[j] != s1[i]
&& arr[j] != s2[i]) {
s3[i] = arr[j];
T--;
break ;
}
}
}
else if (X > 0) {
s3[i] = s1[i];
X--;
}
}
}
cout << s3 << endl;
}
else {
for ( int i = 0;
i < s1.size(); i++) {
if (s1[i] != s3[i]) {
for ( int j = 0;
j < 3; j++) {
if (arr[j] != s1[i]
&& arr[j] != s3[i]) {
s3[i] = arr[j];
k--;
break ;
}
}
}
}
for ( int i = 0;
i < s1.size(); i++) {
if (s1[i] == s3[i] && k) {
for ( int j = 0; j < 3; j++) {
if (arr[j] != s1[i]
&& arr[j] != s3[i]) {
s3[i] = arr[j];
k--;
break ;
}
}
}
}
cout << s3 << endl;
}
}
}
int main()
{
int N = 4, k = 2;
string S1 = "zzyy" ;
string S2 = "zxxy" ;
findString(N, k, S1, S2);
return 0;
}
|
Java
import java.util.*;
class GFG{
static char arr[] = { 'a' , 'b' , 'c' };
static void findString( int n, int k,
char []s1,
char []s2)
{
char []s3 = s2;
int d = 0 ;
for ( int i = 0 ;
i < s1.length; i++)
{
if (s1[i] != s2[i])
d++;
}
if ((d + 1 ) / 2 > k)
{
System.out.print( "-1" + "\n" );
return ;
}
else
{
if (k <= d)
{
int X = d - k;
int T = 2 * k - d;
for ( int i = 0 ; i < s3.length; i++)
{
if (s1[i] != s2[i])
{
if (T > 0 )
{
for ( int j = 0 ; j < 3 ; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s2[i])
{
s3[i] = arr[j];
T--;
break ;
}
}
}
else if (X > 0 )
{
s3[i] = s1[i];
X--;
}
}
}
System.out.print( new String(s3) + "\n" );
}
else
{
for ( int i = 0 ;
i < s1.length; i++)
{
if (s1[i] != s3[i])
{
for ( int j = 0 ;
j < 3 ; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break ;
}
}
}
}
for ( int i = 0 ;
i < s1.length; i++)
{
if (s1[i] == s3[i] && k > 0 )
{
for ( int j = 0 ; j < 3 ; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break ;
}
}
}
}
System.out.print( new String(s3) + "\n" );
}
}
}
public static void main(String[] args)
{
int N = 4 , k = 2 ;
String S1 = "zzyy" ;
String S2 = "zxxy" ;
findString(N, k, S1.toCharArray(), S2.toCharArray());
}
}
|
Python3
arr = [ 'a' , 'b' , 'c' ]
def findString(n, k, s1, s2):
s3 = s2
s3 = list (s3)
d = 0
for i in range ( len (s1)):
if (s1[i] ! = s2[i]):
d + = 1
if ((d + 1 ) / / 2 > k):
print ( "-1" )
return
else :
if (k < = d):
X = d - k
T = 2 * k - d
for i in range ( len (s3)):
if (s1[i] ! = s2[i]):
if (T > 0 ):
for j in range ( 3 ):
if (arr[j] ! = s1[i] and
arr[j] ! = s2[i]):
s3[i] = arr[j]
T - = 1
break
elif (X > 0 ):
s3[i] = s1[i]
X - = 1
print ("".join(s3))
else :
for i in range ( len (s1)):
if (s1[i] ! = s3[i]):
for j in range ( 3 ):
if (arr[j] ! = s1[i] and
arr[j] ! = s3[i]):
s3[i] = arr[j]
k - = 1
break
for i in range ( len (s1)):
if (s1[i] = = s3[i] and k):
for j in range ( 3 ):
if (arr[j] ! = s1[i] and
arr[j] ! = s3[i]):
s3[i] = arr[j]
k - = 1
break
print ("".join(s3))
if __name__ = = "__main__" :
N = 4
k = 2
S1 = "zzyy"
S2 = "zxxy"
findString(N, k, S1, S2)
|
C#
using System;
class GFG{
static char []arr = { 'a' , 'b' , 'c' };
static void findString( int n, int k,
char []s1,
char []s2)
{
char []s3 = s2;
int d = 0;
for ( int i = 0;
i < s1.Length; i++)
{
if (s1[i] != s2[i])
d++;
}
if ((d + 1) / 2 > k)
{
Console.Write( "-1" + "\n" );
return ;
}
else
{
if (k <= d)
{
int X = d - k;
int T = 2 * k - d;
for ( int i = 0; i < s3.Length; i++)
{
if (s1[i] != s2[i])
{
if (T > 0)
{
for ( int j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s2[i])
{
s3[i] = arr[j];
T--;
break ;
}
}
}
else if (X > 0)
{
s3[i] = s1[i];
X--;
}
}
}
Console.Write( new String(s3) + "\n" );
}
else
{
for ( int i = 0; i < s1.Length; i++)
{
if (s1[i] != s3[i])
{
for ( int j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break ;
}
}
}
}
for ( int i = 0; i < s1.Length; i++)
{
if (s1[i] == s3[i] && k > 0)
{
for ( int j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break ;
}
}
}
}
Console.Write( new String(s3) + "\n" );
}
}
}
public static void Main(String[] args)
{
int N = 4, k = 2;
String S1 = "zzyy" ;
String S2 = "zxxy" ;
findString(N, k, S1.ToCharArray(),
S2.ToCharArray());
}
}
|
Javascript
<script>
var arr = [ 'a' , 'b' , 'c' ];
function findString(n , k,s1,s2)
{
var s3 = s2;
var d = 0;
for (i = 0;
i < s1.length; i++)
{
if (s1[i] != s2[i])
d++;
}
if ((d + 1) / 2 > k)
{
document.write( "-1" + "<br>" );
return ;
}
else
{
if (k <= d)
{
var X = d - k;
var T = 2 * k - d;
for (i = 0; i < s3.length; i++)
{
if (s1[i] != s2[i])
{
if (T > 0)
{
for (j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s2[i])
{
s3[i] = arr[j];
T--;
break ;
}
}
}
else if (X > 0)
{
s3[i] = s1[i];
X--;
}
}
}
document.write(s3.join( '' ) + "<br>" );
}
else
{
for (i = 0;
i < s1.length; i++)
{
if (s1[i] != s3[i])
{
for (j = 0;
j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break ;
}
}
}
}
for (i = 0;
i < s1.length; i++)
{
if (s1[i] == s3[i] && k > 0)
{
for (j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break ;
}
}
}
}
document.write(s3.join( '' ) + "<br>" );
}
}
}
var N = 4, k = 2;
var S1 = "zzyy" ;
var S2 = "zxxy" ;
findString(N, k, S1.split( '' ), S2.split( '' ));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...