题解 | #查找两个字符串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
    // while(line = await readline()){
    //     let tokens = line.split(' ');
    //     let a = parseInt(tokens[0]);
    //     let b = parseInt(tokens[1]);
    //     console.log(a + b);
    // }
    let a = await readline();
    let b = await readline();

    if (a.length > b.length) {
        [a, b] = [b, a];
    }

    let res = "";
    let j = 1;
    let i = 0;

    while(i < a.length && j < a.length + 1) {
        let temp = a.slice(i, j);
        if(b.indexOf(temp) > -1) {
            res = temp.length > res.length ? temp : res;
            j++
        } else {
            i++;
            j = i + 1;
        }
    }
    console.log(res);
})();

全部评论

相关推荐

牛至超人:哈工大已经很棒了,不需要加括号了,然后咋没有实习经历呢?火速趁寒假整一段实习,导师不让就狠狠肘击
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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