Minimum cost to convert str1 to str2 with the given operations | Function to return the minimum cost to convert str1 to sr2 ; For every character of str1 ; If current character is not equal in both the strings ; If the next character is also different in both the strings then these characters can be swapped ; Change the current character ; Driver code
def minCost ( str1 , str2 , n ) : NEW_LINE INDENT cost = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT if ( i < n - 1 and str1 [ i + 1 ] != str2 [ i + 1 ] ) : NEW_LINE INDENT swap ( str1 [ i ] , str1 [ i + 1 ] ) NEW_LINE cost += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cost += 1 NEW_LINE DEDENT DEDENT DEDENT return cost NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abb " NEW_LINE str2 = " bba " NEW_LINE n = len ( str1 ) NEW_LINE print ( minCost ( str1 , str2 , n ) ) NEW_LINE DEDENT