All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0601_0700.s0647_palindromic_substrings.solution.js Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Medium #String #Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n)
// #2024_12_24_Time_3_ms_(99.10%)_Space_49.8_MB_(85.90%)

/**
 * @param {string} s
 * @return {number}
 */
var countSubstrings = function(s) {
    const expand = (a, l, r, res) => {
        while (l >= 0 && r < a.length) {
            if (a[l] !== a[r]) {
                return
            } else {
                res.count++
                l--
                r++
            }
        }
    }

    const a = s.split('')
    const res = { count: 0 }

    for (let i = 0; i < a.length; i++) {
        expand(a, i, i, res)       // Odd-length palindromes
        expand(a, i, i + 1, res)   // Even-length palindromes
    }

    return res.count
};

export { countSubstrings }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy