• + 0 comments

    Step-by-step Solution in C++ :-

    1.Find the common prefix length.

    2.Calculate how many characters need to be deleted from s and added to reach t.

    3.Check if k is enough to do that and whether the leftover operations can be used meaninglessly.

    string appendAndDelete(string s, string t, int k) {
        int commonLength = 0;
        int n1 = s.size(), n2 = t.size();
    
        // Find common prefix
        for (int i = 0; i < min(n1, n2); i++) {
            if (s[i] == t[i])
                commonLength++;
            else
                break;
        }
    
        int totalOps = (n1 - commonLength) + (n2 - commonLength);
    
        // Case 1: Enough operations and parity matches
        if (totalOps == k)
            return "Yes";
    
        // Case 2: More operations than needed
        if (totalOps < k) {
            // Check if we can use extra operations meaninglessly
            if ((k - totalOps) % 2 == 0 || k >= n1 + n2)
                return "Yes";
        }
    
        return "No";
    }