• + 0 comments

    Simple C++ solution with the concepts involucred.

    string appendAndDelete(string s, string t, int k) {
        if(k >= (s.length() + t.length())) return "Yes";
        int i = 0;
        int sz = min(s.length(), t.length());
        // longest same prefix
        for(;i<sz;i++){
            if(s[i] != t[i]){
                break;
            }
        }
        int n = s.length();
        int m = t.length();
        int deleteOpe = n - i;
        int addOpe = max(0, m - i);
        int totOpe = deleteOpe + addOpe;
        // why the k-totOpe % 2 == 0? because must be EXACTLY k operations.
        if(k >= totOpe && (k-totOpe)%2 == 0){
            return "Yes";
        }
        return "No";
    }