题解 | #查找两个字符串a,b中的最长公共子串#

查找两个字符串a,b中的最长公共子串

https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506

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
    let lines = [];
    while ((line = await readline())) {
         lines.push(line);
        if(lines.length===2){
            getMax(lines);
        }
    }
})();

function getMax(lines) {
    let short = lines[0];
    let long = lines[1];
    if (short.length > long.length) {
        short = lines[1];
        long = lines[0];
    }
    let maxstr = short[0];
    let max = 1;
    for (let i = 0; i < short.length; i++) {
        for (let j = 0; j <= i; j++) {
            let str = short.slice(j, i + 1);
            if (long.includes(str)) {
                let len = str.length;
                if (len > max) {
                    max = len;
                    maxstr = str;
                }
            }
        }
    }
    console.log(maxstr);
}

全部评论

相关推荐

qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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