给定两个字符串s1,s2,写函数输出s1和s2的最长公共子串

var subS="";
var subLen=0;
var s1="dlajadoueowfljlj0000123456ldjfl1111111111111alfj";
var s2="lfjaldjuaoewnn0000123456jdfjsiol1111111111111alf";
for(let i=0;i<s1.length;i++){        
    for(let j=1;j<=s1.length-i;j++){
        if(s2.indexOf(s1.substr(i,j))<0) {
            if(j-1>subLen){
                subLen=j-1;
                substr=s1.substr(i,j-1);
            }
            break;
        }
    }
}
console.log(substr,subLen);//l1111111111111alf 17

 

posted @ 2022-05-13 10:54  JackieDYH  阅读(4)  评论(0编辑  收藏  举报  来源