Make a given Binary String non | Function to return the length of smallest subsequence required to be removed to make the given string non - decreasing ; Length of the string ; Count of zeros and ones ; Traverse the string ; Count minimum removals to obtain strings of the form "00000 . . . . " or "11111 . . . " ; Increment count ; Remove 1 s and remaining 0 s ; Driver Code
function min_length ( str ) { var n = str . length ; var total_zeros = 0 ; var total_ones = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) total_zeros ++ ; else total_ones ++ ; } var ans = Math . min ( total_zeros , total_ones ) ; var cur_zeros = 0 , cur_ones = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) cur_zeros ++ ; else cur_ones ++ ; ans = Math . min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) ; } document . write ( ans ) ; } var str = " " ; min_length ( str ) ;