Sort by

recency

|

1448 Discussions

|

  • + 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";
    }
    
  • + 0 comments

    Java ::

    public static String appendAndDelete(String s, String t, int k) { // Write your code here int n = s.length(); int m = t.length(); int i = 0; char[] a = s.toCharArray();

            char[] b = t.toCharArray();
    
    
    while (i < n && i < m && a[i] == b[i]) {
        i++;
    }
    
    int opsNeeded = (n - i) + (m - i);
    
    if (opsNeeded > k) {
        return "No";
    }
    else if ((k - opsNeeded) % 2 == 0 || k >= n + m) {
        return "Yes";
    }
    else {
        return "No";
    }
    }
    
  • + 1 comment

    if i´m not mistaken these tests are wrong, please if i'm wrong help me to understand my mistake

            Input (stdin):                    s:abcd, t:abcdert, k:10
            Expected Output:            No
    
            I think having k = 3 here is enough so the out put should be Yes
    
            Input (stdin):                  s:y, t:yu,  k = 2
            Expected Output:         No
            having  k = 1 is enough and the output should be Yes
    
  • + 0 comments

    Thing is, you need to use exactly k moves, no more, no less. Else, fail.

    string appendAndDelete(string s, string t, int k) {
        int n = s.size();
        int m = t.size();
        int i = 0;
    
        while (i < n && i < m && s[i] == t[i]) {
            i++;
        }
    
        int opsNeeded = (n - i) + (m - i);
    
        if (opsNeeded > k) {
            return "No";
        }
        else if ((k - opsNeeded) % 2 == 0 || k >= n + m) {
            return "Yes";
        }
        else {
            return "No";
        }
    }
    
  • + 1 comment

    The test case 7 is wrong

    s=aaaaaaaaaa t=aaaaa k=7

    You only need 5 delete operations on s to convert t

    But the test case expect "Yes"