Minimum substring flips required to convert given binary string to another | Function that finds the minimum number of operations required such that string A and B are the same ; Stores the count of steps ; Stores the last index whose bits are not same ; Iterate until both string are unequal ; Check till end of string to find rightmost unequals bit ; Update the last index ; Flipping characters up to the last index ; Flip the bit ; Increasing steps by one ; Print the count of steps ; Driver Code ; Given strings A and B ; Function Call
def findMinimumOperations ( a , b ) : NEW_LINE INDENT step = 0 NEW_LINE last_index = 0 NEW_LINE while ( a != b ) : NEW_LINE INDENT a = [ i for i in a ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT if ( a [ i ] != b [ i ] ) : NEW_LINE INDENT last_index = i NEW_LINE DEDENT DEDENT for i in range ( last_index + 1 ) : NEW_LINE INDENT if ( a [ i ] == '0' ) : NEW_LINE INDENT a [ i ] = '1' NEW_LINE DEDENT else : NEW_LINE INDENT a [ i ] = '0' NEW_LINE DEDENT DEDENT a = " " . join ( a ) NEW_LINE step += 1 NEW_LINE DEDENT print ( step ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = "101010" NEW_LINE B = "110011" NEW_LINE findMinimumOperations ( A , B ) NEW_LINE DEDENT