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.
Input: S = “101100101”, K = 3
Flip the ‘0’ at 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
Flip the ‘0’ and ‘1’ at index 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.
The problem can be solved using Greedy Approach.
Follow the steps below:
- Iterate the given string with increments by 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:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum flips required to keep all 1s together in a Binary string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Split a given string into substrings of length K with equal sum of ASCII values
- Minimum number of given operations required to convert a string to another string
- Minimum number of subsequences required to convert one string to another
- Minimum swaps required to convert one binary string to another
- Minimum given operations required to convert a given binary string to all 1's
- Minimum operations required to convert a binary string to all 0s or all 1s
- Lexicographical concatenation of all substrings of a string
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum concatenation required to get strictly LIS for the given array
- Minimum concatenation required to get strictly LIS for array with repetitive elements | Set-2
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Convert all substrings of length 'k' from base 'b' to decimal
- Number of ways of choosing K equal substrings of any length for every query
- Repeat substrings of the given String required number of times
- Check if a string can be split into even length palindromic substrings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.