Minimum number of flips to make a Binary String increasing | 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 ; Stores the minimum count of flips ; Traverse the given string S ; Update the value of res and count of 1 s ; Return the minimum number of flips ; Given String ; Function Call
function minimumFlips ( s ) { let n = s . length ; let i ; let cnt0 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) cnt0 ++ ; } let cnt1 = 0 let res = n - cnt0 for ( i = 0 ; i < n ; i ++ ) if ( s [ i ] == ' ' ) { cnt0 -= 1 } else if ( s [ i ] == ' ' ) { res = Math . min ( res , cnt1 + cnt0 ) cnt1 += 1 } return res } S = ' ' document . write ( minimumFlips ( S ) )