Sort by

recency

|

1409 Discussions

|

  • + 0 comments

    python:

    def appendAndDelete(s, t, k):
        # Write your code here
        if len(s)+len(t)<=k:
            return 'Yes'
        i = 0
        while i < min(len(s), len(t)) and s[i] == t[i]:
            i += 1
        root_index = i
        delta = len(s) - root_index
        chars_to_add = len(t) - root_index
        remaining_ops = k-(delta+chars_to_add)
        if remaining_ops>=0 and remaining_ops%2==0: 
            return 'Yes'
        else: 
            return 'No'
    
  • + 0 comments

    Hi I tried with no more than a simple if else and I could, just my logic is kind of weird but works.

    public static String appendAndDelete(String s, String t, int k) {
        // Write your code here
            
            int uno = s.length();
            int dos = t.length();
            
            if (s.equals(t))
            {return "Yes";}
            else if (s.length()>t.length() & (uno -dos) > k ){
                
                return "No";
            }else if (s.length()<t.length() & (dos-uno) == k/2 & s.charAt(uno-1) == t.charAt(dos-1)){
                
                return "Yes";
            }
            else if (s.length()<t.length() & (dos-uno) < k){
                
                return "No";
            }
            else if (s.length()== t.length() & (uno/2) > k/2 ){
                
                return "No";
            }else if (s.charAt(0)!= t.charAt(0) && s.length()>k/2){
                return "No";
            }
            
            return "Yes";
    
        }
    
    }
    
  • + 1 comment

    Why is the test case below is expected as "No"? y yu 2

    Just remove/append the u

  • + 0 comments

    C++ solution

    string appendAndDelete(string s, string t, int k) {
      
      int sl = s.length();
      int tl = t.length();
      
      if(k > (sl + tl))
        return "Yes";
      
      int length = 0;
      int diffLength = 0;
      if(sl >= tl) {
        length = tl;
        diffLength = sl - tl;
      }
      else {
        length = sl;
        diffLength = tl - sl;
      }
      if(diffLength > k)
        return "No";
      
      int diffIndex = length;
      for(int i = 0; i < length; i++) {
        if(s[i] != t[i]) {
          diffIndex = i;
          break;
        }
      }
      
      int operations = (sl + tl - 2*diffIndex);
      if(operations == k || (operations % 2 == k % 2 && operations < k))
        return "Yes";
        
      return "No";
    }
    
  • + 0 comments

    TS Solution -- optimal time complexity but not optimised:

    function appendAndDelete(s: string, t: string, k: number): string { // Write your code here

    for(let i = 0; i < Math.max(s.length, t.length); i++) {
    
        if(s[i] != t[i]) {
    
            let numCharsToDelete:number = s.length - i
            let numCharsToRebuild:number = t.length - i
            let minActionsToZeroAndBack:number = (s.length - numCharsToDelete) * 2
    
            k -= numCharsToDelete + numCharsToRebuild
    
            if (k >= 0 && (k % 2 === 0 || minActionsToZeroAndBack <= k )) {
                return "Yes"
            } else {
                return "No"
            }
        }
    }
    
    return "Yes" 
    

    }