Minimum number of operations required to maximize the Binary String | Function to find the number of operations required ; Count of 1 's ; Count of 0 's upto (cnt1)-th index ; Return the answer ; Driver Code
def minOperation ( s , n ) : NEW_LINE INDENT cnt1 = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ord ( s [ i ] ) == ord ( '1' ) ) : NEW_LINE INDENT cnt1 += 1 ; NEW_LINE DEDENT DEDENT cnt0 = 0 ; NEW_LINE for i in range ( 0 , cnt1 ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT cnt0 += 1 ; NEW_LINE DEDENT DEDENT return cnt0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE s = "01001011" ; NEW_LINE ans = minOperation ( s , n ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT