Minimum flips required to convert given string into concatenation of equal substrings of length K
Last Updated :
28 May, 2021
Given a binary string S and an integer K, the task is to find the minimum number of flips required to convert the given string into a concatenation of K-length equal sub-strings. It is given that the given string can be split into K-length substrings.
Examples:
Input: S = “101100101”, K = 3
Output: 1
Explanation:
Flip the ‘0’ from index 5 to ‘1’.
The resultant string is S = “101101101”.
It is the concatenation of substring “101”.
Hence, the minimum number of flips required is 1.
Input: S = “10110111”, K = 4
Output: 2
Explanation:
Flip the ‘0’ and ‘1’ at indexes 4 and 5 respectively.
The resultant string is S = “10111011”.
It is the concatenation of the substring “1011”.
Hence, the minimum number of flips required is 2.
Approach:
The problem can be solved using Greedy Approach.
Follow the steps below:
- Iterate the given string with increments of K indices from each index and keep a count of the 0s and 1s.
- The character which occurs the minimum number of times must be flipped and keep incrementing that count.
- Perform the above steps for all the indices from 0 to K-1 to obtain the minimum number of flips required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations(string S, int K)
{
int ans = 0;
for ( int i = 0; i < K; i++) {
int zero = 0, one = 0;
for ( int j = i;
j < S.size(); j += K) {
if (S[j] == '0' )
zero++;
else
one++;
}
ans += min(zero, one);
}
return ans;
}
int main()
{
string S = "110100101" ;
int K = 3;
cout << minOperations(S, K);
return 0;
}
|
Java
import java.io.*;
class GFG{
public static int minOperations(String S, int K)
{
int ans = 0 ;
for ( int i = 0 ; i < K; i++)
{
int zero = 0 , one = 0 ;
for ( int j = i; j < S.length(); j += K)
{
if (S.charAt(j) == '0' )
zero++;
else
one++;
}
ans += Math.min(zero, one);
}
return ans;
}
public static void main(String args[])
{
String S = "110100101" ;
int K = 3 ;
System.out.println(minOperations(S, K));
}
}
|
Python3
def minOperations(S, K):
ans = 0
for i in range (K):
zero, one = 0 , 0
for j in range (i, len (S), K):
if (S[j] = = '0' ):
zero + = 1
else :
one + = 1
ans + = min (zero, one)
return ans
if __name__ = = '__main__' :
s = "110100101"
K = 3
print (minOperations(s, K))
|
C#
using System;
class GFG{
public static int minOperations(String S, int K)
{
int ans = 0;
for ( int i = 0; i < K; i++)
{
int zero = 0, one = 0;
for ( int j = i; j < S.Length; j += K)
{
if (S[j] == '0' )
zero++;
else
one++;
}
ans += Math.Min(zero, one);
}
return ans;
}
public static void Main(String []args)
{
String S = "110100101" ;
int K = 3;
Console.WriteLine(minOperations(S, K));
}
}
|
Javascript
<script>
function minOperations(S, K) {
var ans = 0;
for ( var i = 0; i < K; i++) {
var zero = 0,
one = 0;
for ( var j = i; j < S.length; j += K) {
if (S[j] === "0" )
zero++;
else
one++;
}
ans += Math.min(zero, one);
}
return ans;
}
var S = "110100101" ;
var K = 3;
document.write(minOperations(S, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...