Sort by

recency

|

607 Discussions

|

  • + 0 comments

    I've passed, so this is a moot point, but doesn't the problem statement say you can "delete all of the remaining lowercase letters in a"? This implies that if you start deleting, you have to keep deleting: "all", not "some". Yet this test case does not fail as it should:

    {"abCd", "Cd", false},

  • + 0 comments

    Anyone have a non-code description of how to do this ? Nothing I've tried has worked.

  • + 1 comment

    Python 3 solution

    def abbreviation(a, b):
        # Write your code here
        dp=[ [1]+ [0]*(len(b)) for i in range(len(a)+1)]
        # dp[i][0] always 1('YES') cuz b got 0 length
        
        for i in range(1,len(a)+1):
            for j in range(1,len(b)+1):
                if a[i-1]==b[j-1]:
                    # same upper case
                    dp[i][j]=dp[i-1][j-1]
                elif a[i-1]==b[j-1].lower():
                    # same lower case
                    dp[i][j]=max(dp[i-1][j-1], dp[i-1][j])
                elif a[i-1].isupper():
                    dp[i][j]=0
                else:
                    dp[i][j]=dp[i-1][j]
    
        if dp[len(a)][len(b)]:
            return 'YES'
        else:
            return 'NO'
    
  • + 1 comment

    Its difficult

  • + 0 comments

    tricky question. the naive dynamic programming 2D matrix method takes O(a.size()^2 * b.size()), which will likely give time out, but with modification can do it in O(a.size() * b.size()) time

    string abbreviation(string a, string b) {
        vector<vector<bool>> M(a.size()+1, vector<bool>(b.size()+1));
        int upperCase = 0;
        M[0][0] = true;
        for (int i=1; i < a.size()+1; i++) {
            if (isupper(a[i-1])) upperCase++;
            if (upperCase == 0) M[i][0] = true;
        }
        for (int j=1; j < b.size()+1; j++) {
            for (int i=j; i < a.size()+1; i++) {
                if (M[i-1][j] == false or isupper(a[i-1])) M[i][j] = (M[i-1][j-1] and toupper(a[i-1]) == b[j-1]);
                else M[i][j] = true;
            }
        }
        return M[a.size()][b.size()] ? "YES" : "NO";
    }