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.
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:
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 firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generate string with Hamming Distance as half of the hamming distance between strings A and B
- Lexicographically smallest string which differs from given strings at exactly K indices
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Find a rotation with maximum hamming distance
- Reduce Hamming distance by swapping two characters
- Hamming Distance between two strings
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Print the lexicographically smallest array by swapping elements whose sum is odd
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest permutation of a string with given subsequences
- Find the lexicographically smallest string which satisfies the given condition
- Lexicographically smallest subsequence possible by removing a character from given string
- Lexicographically smallest string possible by inserting given character
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Hamming Code implementation in Python
- Hamming code Implementation in C/C++
- Lexicographically Kth smallest way to reach given coordinate from origin