题解 | #查找两个字符串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 () {
    const str1 = await readline();
    const str2 = await readline();
    const [longStr,shortStr] = str1.length > str2.length?[str1,str2]:[str2,str1];
    const len = shortStr.length;
    for(let i = len; i > 0; i--){
        for(j = 0; j + i <= len; j++){
           if(longStr.includes(shortStr.substring(j,j+i))) return console.log(shortStr.substring(j,j+i));
        }
    }
}()

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务