Lexicographically smallest string whose hamming distance from given string is exactly K

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 character at current position of string A is ‘a’, then we assign current position of string B character ‘a’. This position will not contribute towards 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 atmost k times because Hamming distance have 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 previous step, hamming distance between A and B is K, we aare done otherwise we have to make more changes to B. Now we will start from right to left in B, and if character at current position is equal to corresponding character of A, change character of B to ‘b’, hence increasing the hamming distanceby one, we will do it until hamming distance becomes equal to K.

Below is the implementation of this approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find Lexicographically
// smallest string whose hamming distance
// from given string is exactly K
#include <bits/stdc++.h>
using namespace std;
  
// function to find Lexicographically
// smallest string with hamming distance k
void findString(string str, int n, int k)
{
    // If k is 0, output input string
    if (k == 0) {
        cout << str << endl;
        return;
    }
  
    // Copying input string into output
    // string
    string str2 = str;
    int p = 0;
  
    // Traverse all the character of the
    // string
    for (int i = 0; i < n; i++) {
      
        // If current charcter is not 'a'
        if (str2[i] != 'a') {
      
            // copy character 'a' to 
            // output string
            str2[i] = 'a';
            p++;
  
            // If hamming distance became k,
            // break;
            if (p == k)
                break;
        }
    }
  
    // If k is less than p
    if (p < k) {
          
        // Traversing string in reverse 
        // order
        for (int i = n - 1; i >= 0; i--)
            if (str[i] == 'a') {
                str2[i] = 'b';
                p++;
  
                if (p == k)
                    break;
            }
    }
  
    cout << str2 << endl;
}
  
// Driven Program
int main()
{
    string str = "pqrs";
    int n = str.length();
    int k = 2;
  
    findString(str, n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find Lexicographically 
// smallest string whose hamming distance 
// from given string is exactly K  
  
class GFG {
  
// function to find Lexicographically 
// smallest string with hamming distance k 
static void findString(String str, int n, int k) 
    // If k is 0, output input string 
    if (k == 0) { 
                  System.out.println(str);; 
        return
    
  
    // Copying input string into output 
    // string 
    String str2 = str; 
    int p = 0
  
    // Traverse all the character of the 
    // string 
    for (int i = 0; i < n; i++) { 
      
        // If current charcter is not 'a' 
        if (str2.charAt(i) != 'a') { 
      
            // copy character 'a' to 
            // output string 
                        str2 = str2.substring(0,i)+'a'+str2.substring(i+1);
            //str2[i] = 'a'; 
            p++; 
  
            // If hamming distance became k, 
            // break; 
            if (p == k) 
                break
        
    
  
    // If k is less than p 
    if (p < k) { 
          
        // Traversing string in reverse 
        // order 
        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); 
  
// Driven Program 
 public static void main(String[] args) {
  
        String str = "pqrs"
    int n = str.length(); 
    int k = 2
  
    findString(str, n, k); 
  
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python 3

# Python 3 program to find Lexicographically
# smallest string whose hamming distance
# from given string is exactly K

# function to find Lexicographically
# smallest string with hamming distance k
def findString(str, n, k):

# If k is 0, output input string
if (k == 0):
print(str)
return

# Copying input string into output
# string
str2 = str
p = 0

# Traverse all the character of the
# string
for i in range(0, n, 1):

# If current charcter is not ‘a’
if (str2[i] != ‘a’):

# copy character ‘a’ to
# output string
str2 = str2.replace(str2[i], ‘a’)
p += 1

# If hamming distance became k,
# break;
if (p == k):
break

# If k is less than p
if (p < k): # Traversing string in reverse # order 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)

# Driver Code
if __name__ == ‘__main__’:
str = “pqrs”
n = len(str)
k = 2

findString(str, n, k)

# This code is contributed by
# Surendra_Gangwar

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find Lexicographically 
// smallest string whose hamming distance 
// from given string is exactly K 
using System;
  
class GFG
{
  
// function to find Lexicographically 
// smallest string with hamming distance k 
static void findString(String str, 
                       int n, int k) 
    // If k is 0, output input string 
    if (k == 0) 
    
        Console.Write(str);; 
        return
    
  
    // Copying input string into  
    // output string 
    String str2 = str; 
    int p = 0; 
  
    // Traverse all the character  
    // of the string 
    for (int i = 0; i < n; i++)
    
      
        // If current charcter is not 'a' 
        if (str2[i] != 'a')
        
      
            // copy character 'a' to 
            // output string 
            str2 = str2.Substring(0, i) + 'a'
                   str2.Substring(i + 1);
            //str2[i] = 'a'; 
            p++; 
  
            // If hamming distance became k, 
            // break; 
            if (p == k) 
                break
        
    
  
    // If k is less than p 
    if (p < k) 
    
          
        // Traversing string in reverse 
        // order 
        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); 
  
// Driver Code
public static void Main()
{
    String str = "pqrs"
    int n = str.Length; 
    int k = 2; 
  
    findString(str, n, k); 
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find Lexicographically
// smallest string whose hamming distance
// from given string is exactly K
  
// function to find Lexicographically
// smallest string with hamming distance k
function findString($str, $n, $k)
{
    // If k is 0, output input string
    if ($k == 0) 
    {
        echo $str . "\n";
        return;
    }
  
    // Copying input string into 
    // output string
    $str2 = $str;
    $p = 0;
  
    // Traverse all the character 
    // of the string
    for ($i = 0; $i < $n; $i++)
    {
      
        // If current charcter is not 'a'
        if ($str2[$i] != 'a'
        {
      
            // copy character 'a' to 
            // output string
            $str2[$i] = 'a';
            $p++;
  
            // If hamming distance became k,
            // break;
            if ($p == $k)
                break;
        }
    }
  
    // If k is less than p
    if ($p < $k
    {
          
        // Traversing string in reverse 
        // order
        for ($i = $n - 1; $i >= 0; $i--)
            if ($str[$i] == 'a'
            {
                $str2[$i] = 'b';
                $p++;
  
                if ($p == $k)
                    break;
            }
    }
  
    echo $str2 . "\n";
}
  
// Driver Code
$str = "pqrs";
$n = strlen($str);
$k = 2;
  
findString($str, $n, $k);
  
// This code is contributed 
// by Akanksha Rai
?>

chevron_right



Output:

aars

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up