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++ program for the above approach; #include <bits/stdc++.h> using namespace std;
// Function to find the minimum number of // flips required to make string increasing int minimumFlips(string s)
{ // Length of s
int n = s.size();
// Total number of zero in s
int cnt0 = count(s.begin(), s.end(), '0' );
// Stores count of 1s till ith index
int cnt1 = 0;
// stores the minimum count of flip
int res = n - cnt0;
// Traverse the given string
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
cnt0 -= 1;
}
// Update the value of res
// and count of 1s
else if (s[i] == '1' ) {
res = min(res, cnt1 + cnt0);
cnt1++;
}
}
// Return the minimum number
// of flips
return res;
} // Driver code int main()
{ // Given string
string S = "000110" ;
// function call
cout << minimumFlips(S);
return 0;
} // This code is contributed by parthmanchanda81 |
// Java program for the above approach; import java.util.*;
class GFG
{ // Function to find the minimum number of
// flips required to make String increasing
static int minimumFlips(String s) {
// Length of s
int n = s.length();
// Total number of zero in s
int cnt0 = count(s, '0' );
// Stores count of 1s till ith index
int cnt1 = 0 ;
// stores the minimum count of flip
int res = n - cnt0;
// Traverse the given String
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '0' ) {
cnt0 -= 1 ;
}
// Update the value of res
// and count of 1s
else if (s.charAt(i) == '1' ) {
res = Math.min(res, cnt1 + cnt0);
cnt1++;
}
}
// Return the minimum number
// of flips
return res;
}
private static int count(String s, char c) {
int ans = 0 ;
for ( char i : s.toCharArray())
if (c == i)
ans++;
return ans;
}
// Driver code
public static void main(String[] args) {
// Given String
String S = "000110" ;
// function call
System.out.print(minimumFlips(S));
}
} // This code is contributed by Princi Singh |
# Python program for the above approach # Function to find the minimum number of # flips required to make string increasing def minimumFlips(s):
# Length of s
n = len (s)
# Total number of zero in s
cnt0 = s.count( '0' )
# Stores count of 1s till ith index
cnt1 = 0
# Stores the minimum count of flips
res = n - cnt0
# Traverse the given string S
for i in range (n):
if s[i] = = '0' :
cnt0 - = 1
elif s[i] = = '1' :
# Update the value of res
# and count of 1s
res = min (res, cnt1 + cnt0)
cnt1 + = 1
# Return the minimum number
# of flips
return res
# Driver Code S = '000110'
# Function Call print (minimumFlips(S))
|
using System;
public class GFG {
// Function to find the minimum number of
// flips required to make String increasing
static int minimumFlips(String s)
{
// Length of s
int n = s.Length;
// Total number of zero in s
int cnt0 = count(s, '0' );
// Stores count of 1s till ith index
int cnt1 = 0;
// stores the minimum count of flip
int res = n - cnt0;
// Traverse the given String
for ( int i = 0; i < n; i++) {
if (s[i] == '0' ) {
cnt0 -= 1;
}
// Update the value of res
// and count of 1s
else if (s[i] == '1' ) {
res = Math.Min(res, cnt1 + cnt0);
cnt1++;
}
}
// Return the minimum number
// of flips
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;
}
// Driver code
static public void Main()
{
// Given String
String S = "000110" ;
// function call
Console.Write(minimumFlips(S));
}
} // This code is contributed by maddler. |
<script> // JavaScript program for the above approach;
// Function to find the minimum number of
// flips required to make string increasing
function minimumFlips(s) {
// Length of s
let n = s.length;
// Total number of zero in s
let i;
let cnt0 = 0;
for (i = 0; i < n; i++) {
if (s[i] == '0' )
cnt0++;
}
// Stores count of 1s till ith index
let cnt1 = 0
// Stores the minimum count of flips
let res = n - cnt0
// Traverse the given string S
for (i = 0; i < n; i++)
if (s[i] == '0' ) {
cnt0 -= 1
}
else if (s[i] == '1' ) {
// Update the value of res
// and count of 1s
res = Math.min(res, cnt1 + cnt0)
cnt1 += 1
}
// Return the minimum number
// of flips
return res
}
// Driver Code
S = '000110'
// Function Call
document.write(minimumFlips(S))
// This code is contributed by Potta Lokesh </script>
|
1
Time Complexity: O(N)
Auxiliary Space: O(1)