Maximize the number of times a character can be removed from substring 01 from given Binary String | Function to find the maximum moves that can be performed on a string ; Stores 0 s in suffix ; Stores 1 s in prefix ; Iterate over the characters of the string ; Iterate until i is greater than or equal to 0 ; If N is equal to x + y ; Return answer ; Driver code ; Input ; Function call
def maxOperations ( S , N ) : NEW_LINE INDENT X = 0 NEW_LINE Y = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT break NEW_LINE DEDENT Y += 1 NEW_LINE DEDENT i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT break NEW_LINE DEDENT X += 1 NEW_LINE DEDENT if ( N == X + Y ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return N - ( X + Y ) - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "001111" NEW_LINE N = len ( S ) NEW_LINE print ( maxOperations ( S , N ) ) NEW_LINE DEDENT