题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here while ((line = await readline())) { let max = 0; let arr1 = line.split(""); let arr2 = []; for (let i = 0; i < line.length - 1; i++) { let char = arr1.shift(); //找回文数是奇数时,比如 ...cbabc... 这种情况 max = Math.max(getCount(arr1, arr2) + 1, max); //找回文数为偶数时,比如 ...cbaabc... 这种情况 arr2.unshift(char); max = Math.max(getCount(arr1, arr2), max); } console.log(max); } })(); // 计算当前数组从左到右相同序列的项数和 var getCount = function (arr1 = [], arr2 = []) { let count = 0; let k = Math.min(arr1.length, arr2.length); for (let j = 0; j < k; j++) { if (arr2[j] != arr1[j]) { break; } count++; } return count * 2; };