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++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function that returns the minimum
// number of flips to convert
// the s into a concatenation
// of K-length sub-string
int minOperations(string S, int K)
{
// Stores the result
int ans = 0;
// Iterate through string index
for (int i = 0; i < K; i++) {
// Stores count of 0s & 1s
int zero = 0, one = 0;
// Iterate making K jumps
for (int j = i;
j < S.size(); j += K) {
// Count 0's
if (S[j] == '0')
zero++;
// Count 1's
else
one++;
}
// Add minimum flips
// for index i
ans += min(zero, one);
}
// Return minimum number
// of flips
return ans;
}
// Driver Code
int main()
{
string S = "110100101";
int K = 3;
cout << minOperations(S, K);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function that returns the minimum
// number of flips to convert
// the s into a concatenation
// of K-length sub-string
public static int minOperations(String S, int K)
{
// Stores the result
int ans = 0;
// Iterate through string index
for(int i = 0; i < K; i++)
{
// Stores count of 0s & 1s
int zero = 0, one = 0;
// Iterate making K jumps
for(int j = i; j < S.length(); j += K)
{
// Count 0's
if (S.charAt(j) == '0')
zero++;
// Count 1's
else
one++;
}
// Add minimum flips
// for index i
ans += Math.min(zero, one);
}
// Return minimum number
// of flips
return ans;
}
// Driver Code
public static void main(String args[])
{
String S = "110100101";
int K = 3;
System.out.println(minOperations(S, K));
}
}
// This code is contributed by grand_master
Python3
# Python3 program to implement
# the above approach
# Function that returns the minimum
# number of flips to convert the s
# into a concatenation of K-length
# sub-string
def minOperations(S, K):
# Stores the result
ans = 0
# Iterate through string index
for i in range(K):
# Stores count of 0s & 1s
zero, one = 0, 0
# Iterate making K jumps
for j in range(i, len(S), K):
# Count 0's
if(S[j] == '0'):
zero += 1
# Count 1's
else:
one += 1
# Add minimum flips
# for index i
ans += min(zero, one)
# Return minimum number
# of flips
return ans
# Driver code
if __name__ == '__main__':
s = "110100101"
K = 3
print(minOperations(s, K))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function that returns the minimum
// number of flips to convert
// the s into a concatenation
// of K-length sub-string
public static int minOperations(String S, int K)
{
// Stores the result
int ans = 0;
// Iterate through string index
for(int i = 0; i < K; i++)
{
// Stores count of 0s & 1s
int zero = 0, one = 0;
// Iterate making K jumps
for(int j = i; j < S.Length; j += K)
{
// Count 0's
if (S[j] == '0')
zero++;
// Count 1's
else
one++;
}
// Add minimum flips
// for index i
ans += Math.Min(zero, one);
}
// Return minimum number
// of flips
return ans;
}
// Driver Code
public static void Main(String []args)
{
String S = "110100101";
int K = 3;
Console.WriteLine(minOperations(S, K));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Function that returns the minimum
// number of flips to convert
// the s into a concatenation
// of K-length sub-string
function minOperations(S, K) {
// Stores the result
var ans = 0;
// Iterate through string index
for (var i = 0; i < K; i++) {
// Stores count of 0s & 1s
var zero = 0,
one = 0;
// Iterate making K jumps
for (var j = i; j < S.length; j += K) {
// Count 0's
if (S[j] === "0")
zero++;
// Count 1's
else
one++;
}
// Add minimum flips
// for index i
ans += Math.min(zero, one);
}
// Return minimum number
// of flips
return ans;
}
// Driver Code
var S = "110100101";
var K = 3;
document.write(minOperations(S, K));
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize cost to convert given string into concatenation of equal substrings of length K Given a string S of length N consisting of lowercase letters and an integer K, where N % K = 0, the task is to find the minimum cost to convert the given string into a concatenated string of the same K-length substrings by performing the following operations: A character can be replaced with another
8 min read
Split a given string into substrings of length K with equal sum of ASCII values Given a string str of size N and an integer K, the task is to check if the input string can be partitioned into substrings of size K having a constant sum of ASCII values.Examples: Input: str = "abdcbbdba" K = 3 Output: YES Explanation: 3 length substrings {"and", "cbb", "dba"} with sum of their ASC
6 min read
Convert to a string that is repetition of a substring of k length Given a string, find if it is possible to convert it to a string that is the repetition of a substring with k characters. To convert, we can replace one substring of length k starting at index i (zero-based indexing) such that i is divisible by K, with k characters. Examples: Input: str = "bdac", k
7 min read
Minimum flips required in a binary string such that all K-size substring contains 1 Given a binary string str of size N and a positive integer K, the task is to find the minimum number of flips required to make all substring of size K contain at least one '1'.Examples: Input: str = "0001", K = 2 Output: 1 Explanation: Flipping the bit at index 1 modifies str to "0101". All substrin
11 min read
Minimum cost for constructing the subsequence of length K from given string S Given a string S consisting of N lowercase English alphabets, and an integer K and, an array cost[] of size 26 denoting the cost of each lowercase English alphabet, the task is to find the minimum cost to construct a subsequence of length K from the characters of the string S. Examples: Input: S = "
11 min read