NC17 最长回文子串qaq

97 阅读1分钟

image.png

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param A string字符串
 * @return int整型
 */
function getLongestPalindrome(A) {
    function getLength(begin, end) {
        while (begin >= 0 && end < A.length && A[begin] == A[end]) {
            begin--;
            end++;
        }
        return end - begin - 1; //返回长度
    }

    let maxLen = 1;
    for (let i = 0; i < A.length - 1; i++) {
        // 以每个点为中心,分别计算奇数长度和偶数长度的回文子串长度,并取最大值
        maxLen = Math.max(maxLen, getLength(i, i), getLength(i, i + 1));
    }
    // 返回最长回文子串长度
    return maxLen;
}

module.exports = {
    getLongestPalindrome: getLongestPalindrome,
};