题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
//回马车,把字符扩展成这样的格式 $#a#% ,遍历扩展,i从中间指向两边,计算长度,长度永远是一个奇数字符个数+字符个数+1 长度除2就是字符长度
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
let arr = []
//回马车
void async function () {
// Write your code here
while(line = await readline()){
let str = "%"+"#"+line.split("").join("#")+"#"+"$"
console.log(center(str))
}
}()
function center(str){
let len = str.length;
let max = 0;
for (let i = 2;i<len-1;i++){
let index = i-1;
let endex = i+1;
let length = 0;
while (str[index]==str[endex]){
length = endex-index+1
index--;
endex++;
}
max=Math.max(max,length)
}
return parseInt(max/2)
}