Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0 | Function to find the maximum value of K such that flipping substrings of size at least K make all characters 0 s ; Stores the maximum value of K ; Traverse the given string S ; Store the minimum of the maximum of LHS and RHS length ; Flip performed ; If no flips performed ; Return the possible value of K ; Driver Code
def maximumK ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE ans = N NEW_LINE flag = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( S [ i ] != S [ i + 1 ] ) : NEW_LINE INDENT flag = 1 NEW_LINE ans = min ( ans , max ( i + 1 , N - i - 1 ) ) NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "010" NEW_LINE print ( maximumK ( S ) ) NEW_LINE DEDENT