Sort by

recency

|

1419 Discussions

|

  • + 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.

  • + 0 comments

    My Java 8 solution:

    public static String appendAndDelete(String s, String t, int k) {
        int numMoves = s.length() + t.length();
    
        for (int i = 0; i < s.length() && i < t.length() && s.charAt(i) == t.charAt(i); i++) {
            numMoves -= 2;
        } //see which letters they have in common
    
        if (numMoves > k || (t.length() > s.length() && (numMoves % 2 != 0))) {
            return "No"; //if more than k moves are required to achieve the string
        }
        return "Yes";
    
    }
    
  • + 0 comments

    This is my solution. It's in python. It's probably a bit over complicated but I'll post it anyway

    def appendAndDelete(s, t, k):

    s_list = list(s)
    t_list = list(t)
    changes_needed = 0
    last_the_same = True
    
    if s == t:
        print("Yes")
        return "Yes"
    
    for i in range(len(s)):
        if i < len(t):
            if s[i] == t[i]:
                if last_the_same is False:
                    changes_needed += 1
                else:
                    print(s[i] , " = ", t[i])
            else:
                changes_needed += 1
                last_the_same = False
        else:
            changes_needed += 1
    
    if(changes_needed > 0):
        del s_list[-changes_needed:]
    
    print(" removing from s = ", s_list, ", now adding t... we will add: ", t_list[-changes_needed: ])
    
    for i in range(len(t)):
    
        if i < len(s_list):
            if s_list[i] != t_list[i]:
                s_list.append(t_list[i])
                changes_needed += 1
        else:
            s_list.append(t_list[i])
            changes_needed += 1
    
    
    print(" s with t added = ", s_list, " the number of changes needed was :", changes_needed)
    
    print("k - changes_needed = ", k - changes_needed, ", s_list = ",s_list, ", t_list = ", t_list )
    
    
    if s_list == t_list:
        if len(s) >= len(t):
            if (k - changes_needed) == 0 or (k - changes_needed) > 1:
                print('Yes')
                return "Yes"
            else:
                print('No')
                return "No"
        else:
            if (k - changes_needed) == 0 or (k - changes_needed) % 2 == 0:
                print('Yes')
                return "Yes"
            else:
                print('No')
                return "No"
    else:
        print('No')
        return "No"
    
  • + 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";
    }
    
  • + 0 comments

    My JAVA Solution:

    public static String appendAndDelete(String s, String t, int k) {
        // Write your code here
            int length_s = s.length();
            int length_t = t.length();
            if(k >= length_s + length_t) return "Yes";
            else{
                int differenciator = 0;
                int min_operations = 0;
                
                for (int i = 0; i < (length_s>=length_t?length_t:length_s); i++) {
                    if(s.charAt(i) == t.charAt(i)) differenciator++;
                    else break;
                }
                
                min_operations = (length_s - differenciator) + (length_t - differenciator);
                
                if(k < min_operations) return "No";
                else if(k == min_operations) return "Yes";
                else{
                    if(min_operations%2 == 0){
                        if(k%2 == 0) return "Yes";
                        else return "No";
                    }
                    else{
                        if(k%2 != 0) return "Yes";
                        else return "No";
                    }
                }
            }
        }