Minimum prefixes required to be flipped to convert a Binary String to another | Function to count flips required to make strings A and B equal ; Stores the count of the number of operations ; Stores the length of the chosen prefixes ; Stores if operations are performed even or odd times ; Traverse the given string ; If current characters in the two strings are unequal ; If A [ i ] is not flipped ; Increment count of operations ; Insert the length of the chosen prefix ; Update invert to true ; If A [ i ] is flipped ; Increment count of operations ; Insert length of the chosen prefix ; Update invert to false ; Print the number of operations required ; Print the chosen prefix length in each operation ; Driver Code ; Given binary strings
def findOperations ( A , B , N ) : NEW_LINE INDENT operations = 0 NEW_LINE ops = [ ] NEW_LINE invert = False NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ i ] != B [ i ] ) : NEW_LINE INDENT if ( not invert ) : NEW_LINE INDENT operations += 1 NEW_LINE ops . append ( i + 1 ) NEW_LINE invert = True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( invert ) : NEW_LINE INDENT operations += 1 NEW_LINE ops . append ( i + 1 ) NEW_LINE invert = False NEW_LINE DEDENT DEDENT DEDENT print ( operations ) NEW_LINE if ( operations != 0 ) : NEW_LINE INDENT for x in ops : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B = "001" , "000" NEW_LINE N = len ( A ) NEW_LINE findOperations ( A , B , N ) NEW_LINE DEDENT