• + 0 comments

    Found a peculiar solution I though I would share:

    def appendAndDelete(s, t, k):
        i = 0
        
        # Comment 1
        
        for j in range(len(min(s,t))):
            if s[j] == t[j]:
                i += 1
            else:
                break
                
        # Comment 2
        
        if k >= len(t) + len(s):
            return 'Yes'
         
        # Comment 3
        
        else:
            min_ops = len(t) + len(s) - 2*i
            if min_ops <= k and (k - min_ops)%2 == 0:
                return 'Yes'
            return 'No'
    

    Comment 1

    Calculate the length of the substing of s (starting at the beggining of s) which we do not need to change because it is the same as the corresponding substring of t.

    Comment 2

    If k >= len(t) + len(s) the answer is always yes because we can delete all the letters of s, continue deleting the empty string if k > len(t) + len(s) and then add the elements of t

    Comment 3

    min_ops = len(t) + len(s) - 2*i calculates the minimum number of operations needed to transform s to t. If min_ops > k the answer is no. If min_ops = k the answer is yes. If min_ops < k, the answer is yes if the remaining operations are an even number. In this case, after transforming s we would use the remaining number of operations to add and delete an item (or vice versa) until we use all of k.