• + 3 comments

    Here's a short and sweet JavaScript solution if anybody wants it:

    function appendAndDelete (s, t, k) {
        let o = s.length + t.length
        if (k > o) return 'Yes'
        for (let i = 0, l = Math.min(s.length, t.length); i < l; i++, o -= 2) {
            if (s[i] !== t[i]) break
        }
        return o > k || (k - o) % 2 !== 0 ? 'No' : 'Yes'
    }
    

    Explanation:

    o is the minimum number of operations. But to deal with the one exception in this algorithm, we set it to the total length of both strings first. If the number of allowed operations is bigger than the sum of both strings, the answer will always be "Yes". So we check for that before moving along!

    Now, we compare the letters in both strings and subtract each iteration from the minimum number of operations. Remember that each common letter means 2 less operations (remove a letter + add a letter). We break out of the loop once the letters aren't equal anymore.

    Now, if the minimum number of operations is more than the allowed number, we return "No". Otherwise, as long as the remainder of the difference between minimum and allowed operations is even, we return "Yes".

    Questions? =)