题解 | #最长回文子串#

最长回文子串

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()){
        getResult(line)
    }
}()
function getResult(data) {
    let target = "";
    for (let i = 0; i < data.length; i++) {
        for (let j = i + 1; j < data.length - i + 1; j++) {
            let temp = data.slice(i, j);
            if (isMirror(temp)) {
                target = temp.length > target.length ? temp : target
            }
        }
    }
    console.log(target.length)
}

function isMirror(str) {
    let temp = str.split("").reverse().join("");
    return temp == str;
}

全部评论

相关推荐

02-05 17:50
已编辑
武汉工程科技学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务