题解 | #公共子串计算#

公共子串计算

http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b

思想:短串在长串里 优化:前面的不是子串,加长也不可能是

function getMaxCommonStr(str1,str2) {
    let long = '',short = '';
    if(str1.length > str2.length) {
        long = str1;
        short = str2;
    } else {
        long = str2;
        short = str1;
    }
    let res = 0;
    for(let i = 0; i < short.length; i++) {
        for(let j = i+1; j <= short.length; j++) {
            if(long.indexOf(short.slice(i,j)) !== -1) {
                res = res > j-i?res:j-i;
            } else break;
        }
    }
    return res;
}
const str1 = readline();
const str2 = readline();
const res = getMaxCommonStr(str1,str2);
console.log(res);
全部评论

相关推荐

xdu通信dddd:我小米都面完两个月了 八月底面完的,现在还是显示面试中,没有比我恐怖的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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