Minimize hamming distance in Binary String by setting only one K size substring bits
Last Updated :
10 Aug, 2021
Given two binary strings S and T of length N and a positive integer K. Initially, all characters of T are ‘0’. The task is to find the minimum Hamming distance after choosing a substring of size K and making all elements of string T as ‘1’ only once.
Examples:
Input: S = “101”, K = 2
Output: 1
Explanation: Initially string T = “000”, one possible way is to change all 0s in range [0, 1] to 1. Thus string T becomes “110” and the hamming distance between S and T is 2 which is the minimum possible.
Input: S = “1100”, K=3
Output: 1
Naive Approach: The simplest approach is to consider every substring of size K and make all the elements as 1 and then check the hamming distance with string, S. After checking all the substrings, print the minimum hamming distance.
Time Complexity: O(N×K)
Auxiliary Space: O(1)
Approach: This problem can be solved by creating a prefix array sum which stores the prefix sum of the count of ones in the string S. Follow the steps below to solve the problem:
- Create a prefix sum array pref[] of string S by initializing pref[0] as 0 updating pref[i] as pref[i-1] +(S[i] – ‘0’) for every index i.
- Store the total count of ones in the string, S in a variable cnt.
- Initialize a variable ans as cnt to store the required result.
- Iterate in the range [0, N-K] using the variable i
- Initialize a variable val as pref[i+K-1] – pref[i-1] to store the count of ones in the substring S[i, i+K-1].
- Create two variables A and B to store the hamming distance outside the current substring and the hamming distance inside the current substring and initialize A with cnt – K and B with K – val.
- Update the value of ans with the minimum of ans and (A + B).
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumHammingDistance(string S, int K)
{
int n = S.size();
int pref[n];
pref[0] = S[0] - '0' ;
for ( int i = 1; i < n; i++)
pref[i] = pref[i - 1] + (S[i] - '0' );
int cnt = pref[n - 1];
int ans = cnt;
for ( int i = 0; i < n - K; i++) {
int value = pref[i + K - 1]
- (i - 1 >= 0 ? pref[i - 1] : 0);
ans = min(ans, cnt - value + (K - value));
}
return ans;
}
int main()
{
string s = "101" ;
int K = 2;
cout << minimumHammingDistance(s, K);
return 0;
}
|
Java
public class GFG
{
static int minimumHammingDistance(String S, int K)
{
int n = S.length();
int []pref = new int [n];
pref[ 0 ] = S.charAt( 0 ) - '0' ;
for ( int i = 1 ; i < n; i++)
pref[i] = pref[i - 1 ] + (S.charAt(i) - '0' );
int cnt = pref[n - 1 ];
int ans = cnt;
for ( int i = 0 ; i < n - K; i++) {
int value = pref[i + K - 1 ] - (i - 1 >= 0 ? pref[i - 1 ] : 0 );
ans = Math.min(ans, cnt - value + (K - value));
}
return ans;
}
public static void main(String args[])
{
String s = "101" ;
int K = 2 ;
System.out.println(minimumHammingDistance(s, K));
}
}
|
Python3
def minimumHammingDistance(S, K):
n = len (S)
pref = [ 0 ] * n
pref[ 0 ] = ord (S[ 0 ]) - ord ( '0' )
for i in range ( 1 ,n):
pref[i] = pref[i - 1 ] + ( ord (S[i]) - ord ( '0' ))
cnt = pref[n - 1 ]
ans = cnt
for i in range (n - K):
value = pref[i + K - 1 ] - (pref[i - 1 ] if (i - 1 ) > = 0 else 0 )
ans = min (ans, cnt - value + (K - value))
return ans
if __name__ = = '__main__' :
s = "101"
K = 2
print (minimumHammingDistance(s, K))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int minimumHammingDistance( string S, int K)
{
int n = S.Length;
int []pref = new int [n];
pref[0] = ( int )S[0] - 48;
for ( int i = 1; i < n; i++)
pref[i] = pref[i - 1] + (( int )S[i] - 48);
int cnt = pref[n - 1];
int ans = cnt;
for ( int i = 0; i < n - K; i++) {
int value = pref[i + K - 1] - (i - 1 >= 0 ? pref[i - 1] : 0);
ans = Math.Min(ans, cnt - value + (K - value));
}
return ans;
}
public static void Main()
{
string s = "101" ;
int K = 2;
Console.Write(minimumHammingDistance(s, K));
}
}
|
Javascript
<script>
function minimumHammingDistance(S, K)
{
let n = S.length;
let pref = new Array(n);
pref[0] = S[0] - '0' ;
for (let i = 1; i < n; i++)
pref[i] = pref[i - 1] + (S[i] - '0' );
let cnt = pref[n - 1];
let ans = cnt;
for (let i = 0; i < n - K; i++) {
let value = pref[i + K - 1] - (i - 1 >= 0 ? pref[i - 1] : 0);
ans = Math.min(ans, cnt - value + (K - value));
}
return ans;
}
let s = "101" ;
let K = 2;
document.write(minimumHammingDistance(s, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...