Minimum number of flips to make a Binary String increasing
Last Updated :
07 Mar, 2022
Given a binary string S, the task is to find the minimum number of characters the needed to be flipped to make the given binary string increasing.
Example:
Input: S = “00110”
Output: 1
Explanation: Flip S[4] = ‘0’ to ‘1’ of the string modifies the given string to “00111”. Therefore, the minimum number of flips required is 1.
Input: S = “010110”
Output: 2
Approach: The given problem can be solved by using a Greedy Algorithm based on the observations that the resultant monotonically increasing string after any number of flips will be of the form (‘0’*p + ‘1’*q), where p and q are the count of 0s and 1s respectively in the modified string. The idea is to traverse the given string S and for each index, i modify the substring S[0, i) to 0s and substring S[i, N) to 1s and find the minimum flips required accordingly. Follow the steps below to solve the problem:
- Find the count of 0s in the given binary string S and store it in a variable countZero.
- Initialize variable, say minFlips as (N – cntZero) that stores the minimum number of flips required.
- Initialize variable, say cntOne as 0 that stores the count of 1s in the string while traversing the string.
- Traverse the given string S and perform the following steps:
- If the character S[i] is 0, then decrement the value of countZero by 1.
- Otherwise, update the value of minFlips as the minimum of minFlips and (countZero + countOne) and increment the value of countOne by 1.
- After completing the above steps, print the value of minFlips as the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimumFlips(string s)
{
int n = s.size();
int cnt0 = count(s.begin(), s.end(), '0' );
int cnt1 = 0;
int res = n - cnt0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
cnt0 -= 1;
}
else if (s[i] == '1' ) {
res = min(res, cnt1 + cnt0);
cnt1++;
}
}
return res;
}
int main()
{
string S = "000110" ;
cout << minimumFlips(S);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minimumFlips(String s) {
int n = s.length();
int cnt0 = count(s, '0' );
int cnt1 = 0 ;
int res = n - cnt0;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '0' ) {
cnt0 -= 1 ;
}
else if (s.charAt(i) == '1' ) {
res = Math.min(res, cnt1 + cnt0);
cnt1++;
}
}
return res;
}
private static int count(String s, char c) {
int ans = 0 ;
for ( char i : s.toCharArray())
if (c == i)
ans++;
return ans;
}
public static void main(String[] args) {
String S = "000110" ;
System.out.print(minimumFlips(S));
}
}
|
Python3
def minimumFlips(s):
n = len (s)
cnt0 = s.count( '0' )
cnt1 = 0
res = n - cnt0
for i in range (n):
if s[i] = = '0' :
cnt0 - = 1
elif s[i] = = '1' :
res = min (res, cnt1 + cnt0)
cnt1 + = 1
return res
S = '000110'
print (minimumFlips(S))
|
C#
using System;
public class GFG {
static int minimumFlips(String s)
{
int n = s.Length;
int cnt0 = count(s, '0' );
int cnt1 = 0;
int res = n - cnt0;
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
cnt0 -= 1;
}
else if (s[i] == '1' ) {
res = Math.Min(res, cnt1 + cnt0);
cnt1++;
}
}
return res;
}
private static int count(String s, char c)
{
int ans = 0;
for ( int j = 0; j < s.Length; j++) {
char i = s[j];
if (c == i)
ans++;
}
return ans;
}
static public void Main()
{
String S = "000110" ;
Console.Write(minimumFlips(S));
}
}
|
Javascript
<script>
function minimumFlips(s) {
let n = s.length;
let i;
let cnt0 = 0;
for (i = 0; i < n; i++) {
if (s[i] == '0' )
cnt0++;
}
let cnt1 = 0
let res = n - cnt0
for (i = 0; i < n; i++)
if (s[i] == '0' ) {
cnt0 -= 1
}
else if (s[i] == '1' ) {
res = Math.min(res, cnt1 + cnt0)
cnt1 += 1
}
return res
}
S = '000110'
document.write(minimumFlips(S))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...