Lexicographically smallest string whose hamming distance from given string is exactly K
Last Updated :
22 Jul, 2022
Given a lowercase string A of length N and an integer K, find the lexicographically smallest string B of the same length as A such that hamming distance between A and B is exactly K.
Examples:
Input : A = "pqrs", k = 1.
Output : aqrs
We can differ by at most one
character. So we put 'a' in the
beginning to make the result
lexicographically smallest.
Input : A = "pqrs", k = 2.
Output : aars
We start from left to right, if the character at the current position of string A is ‘a’, then we assign the current position of string B character ‘a’. This position will not contribute towards the hamming distance. If character at this position in A is not equal to ‘a’, then also we will assign current position of string B character ‘a’, now this will contribute towards hamming distance and this can be done at most k times because Hamming distance has to be equal to K, if this is already done K times, we will assign this position of B same character as A.
If after the previous step, hamming distance between A and B is K, we are done otherwise we have to make more changes to B. Now we will start from right to left in B, and if character at the current position is equal to the corresponding character of A, change character of B to ‘b’, hence increasing the hamming distance by one, we will do it until hamming distance becomes equal to K.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findString(string str, int n, int k)
{
if (k == 0) {
cout << str << endl;
return ;
}
string str2 = str;
int p = 0;
for ( int i = 0; i < n; i++) {
if (str2[i] != 'a' ) {
str2[i] = 'a' ;
p++;
if (p == k)
break ;
}
}
if (p < k) {
for ( int i = n - 1; i >= 0; i--)
if (str[i] == 'a' ) {
str2[i] = 'b' ;
p++;
if (p == k)
break ;
}
}
cout << str2 << endl;
}
int main()
{
string str = "pqrs" ;
int n = str.length();
int k = 2;
findString(str, n, k);
return 0;
}
|
Java
class GFG {
static void findString(String str, int n, int k)
{
if (k == 0 ) {
System.out.println(str);;
return ;
}
String str2 = str;
int p = 0 ;
for ( int i = 0 ; i < n; i++) {
if (str2.charAt(i) != 'a' ) {
str2 = str2.substring( 0 ,i)+ 'a' +str2.substring(i+ 1 );
p++;
if (p == k)
break ;
}
}
if (p < k) {
for ( int i = n - 1 ; i >= 0 ; i--)
if (str.charAt(i) == 'a' ) {
str2 = str2.substring( 0 ,i)+ 'b' +str2.substring(i+ 1 );
p++;
if (p == k)
break ;
}
}
System.out.println(str2);
}
public static void main(String[] args) {
String str = "pqrs" ;
int n = str.length();
int k = 2 ;
findString(str, n, k);
}
}
|
Python 3
def findString( str , n, k):
if (k = = 0 ):
print ( str )
return
str2 = str
p = 0
for i in range ( 0 , n, 1 ):
if (str2[i] ! = 'a' ):
str2 = str2.replace(str2[i], 'a' )
p + = 1
if (p = = k):
break
if (p < k):
i = n - 1
while (i > = 0 ):
if ( str [i] = = 'a' ):
str2 = str2.replace(str2[i], 'b' )
p + = 1
if (p = = k):
break
i - = 1
print (str2)
if __name__ = = '__main__' :
str = "pqrs"
n = len ( str )
k = 2
findString( str , n, k)
|
C#
using System;
class GFG
{
static void findString(String str,
int n, int k)
{
if (k == 0)
{
Console.Write(str);;
return ;
}
String str2 = str;
int p = 0;
for ( int i = 0; i < n; i++)
{
if (str2[i] != 'a' )
{
str2 = str2.Substring(0, i) + 'a' +
str2.Substring(i + 1);
p++;
if (p == k)
break ;
}
}
if (p < k)
{
for ( int i = n - 1; i >= 0; i--)
if (str[i] == 'a' )
{
str2 = str2.Substring(0, i) + 'b' +
str2.Substring(i + 1);
p++;
if (p == k)
break ;
}
}
Console.Write(str2);
}
public static void Main()
{
String str = "pqrs" ;
int n = str.Length;
int k = 2;
findString(str, n, k);
}
}
|
PHP
<?php
function findString( $str , $n , $k )
{
if ( $k == 0)
{
echo $str . "\n" ;
return ;
}
$str2 = $str ;
$p = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str2 [ $i ] != 'a' )
{
$str2 [ $i ] = 'a' ;
$p ++;
if ( $p == $k )
break ;
}
}
if ( $p < $k )
{
for ( $i = $n - 1; $i >= 0; $i --)
if ( $str [ $i ] == 'a' )
{
$str2 [ $i ] = 'b' ;
$p ++;
if ( $p == $k )
break ;
}
}
echo $str2 . "\n" ;
}
$str = "pqrs" ;
$n = strlen ( $str );
$k = 2;
findString( $str , $n , $k );
?>
|
Javascript
<script>
function findString(str , n , k)
{
if (k == 0) {
document.write(str);;
return ;
}
var str2 = str;
var p = 0;
for (i = 0; i < n; i++) {
if (str2.charAt(i) != 'a' ) {
str2 = str2.substring(0,i)+ 'a' +str2.substring(i+1);
p++;
if (p == k)
break ;
}
}
if (p < k) {
for (i = n - 1; i >= 0; i--)
if (str.charAt(i) == 'a' ) {
str2 = str2.substring(0,i)+ 'b' +str2.substring(i+1);
p++;
if (p == k)
break ;
}
}
document.write(str2);
}
var str = "pqrs" ;
var n = str.length;
var k = 2;
findString(str, n, k);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...