Minimum number of flips to make a Binary String increasing | Java program for the above approach ; ; Function to find the minimum number of flips required to make String increasing ; Length of s ; Total number of zero in s ; Stores count of 1 s till ith index ; stors the minimum count of flip ; Traverse the given String ; Update the value of res and count of 1 s ; Return the minimum number of flips ; Utility count ; Driver code ; Given String ; function call
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 ) ) ; } }