Minimum flips or swapping of adjacent characters required to make a string equal to another | Function to find minimum operations required to convert A to B ; Store the size of the string ; Store the required result ; Traverse the string , a ; If a [ i ] is equal to b [ i ] ; Check if swapping adjacent characters make the same - indexed characters equal or not ; Otherwise , flip the current bit ; Print the minimum number of operations ; Driver Code ; Given Input ; Function Call
def minimumOperation ( a , b ) : NEW_LINE INDENT n = len ( a ) NEW_LINE i = 0 NEW_LINE minoperation = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( a [ i ] == b [ i ] ) : NEW_LINE INDENT i = i + 1 NEW_LINE continue NEW_LINE DEDENT elif ( a [ i ] == b [ i + 1 ] and a [ i + 1 ] == b [ i ] and i < n - 1 ) : NEW_LINE INDENT minoperation += 1 NEW_LINE i = i + 2 NEW_LINE DEDENT elif ( a [ i ] != b [ i ] ) : NEW_LINE INDENT minoperation += 1 NEW_LINE i = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT print ( minoperation ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = "10010010" NEW_LINE b = "00001000" NEW_LINE minimumOperation ( a , b ) NEW_LINE DEDENT