题解 | #最长回文子串#
最长回文子串
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 // 解题思路 // 采用动态规划, // 记录结果maxSubLength,curSubLength let maxSubLength = 1; while ((str = await readline())) { let length = str.length; // 定义状态:dp[i][j]表示第i到j为回文 const dp = Array.from({ length: length }, () => Array(length).fill(false) ); for (let i = 0; i < length; i++) { dp[i][i] = true; } // console.log(dp); for (let len = 2; len <= length; len++) { for (let i = 0; i <= length-len; i++) { let j=i+len-1; if(str[i]==str[j]&&(len==2||dp[i+1][j-1])){ dp[i][j]=true; // console.log(`sub: ${str.slice(i,j+1)}`) maxSubLength=len; } } } console.log(maxSubLength); } })();