• + 0 comments

    js recursive

    function abbreviation(a, b, i = a.length - 1, j = b.length - 1, dp = {}) {
        if (j < 0) return 'YES';
        if (i < 0) return 'NO';
    
        const k = `${i}-${j}`;
        if (!dp[k]) {
            const c1 = a[i];
            const c2 = b[j];
            const isCap = c1 < 'a';
    
            dp[k] = c1.toUpperCase() === c2
                && abbreviation(a, b, i - 1, j - 1, dp) === 'YES'
                ||
                !isCap
                && abbreviation(a, b, i - 1, j, dp) === 'YES'
                ? 'YES'
                : 'NO';
        }
        return dp[k];
    }