用JS函数写出,输出两个字符串最长的公共部分,如string 和interesting ,输出他们共同的最长的ing
function find(str1,str2){
if(str1.length>str2.length){
shorter = str2;
longer = str1
}else{
shorter = str1;
longer = str2;
}
for(var subLength = shorter.length;subLength>0;subLength--){
for(var i = 0;i+subLength<=shorter.length;i++){
var subString =shorter.substring(i,i+subLength);
// debugger;
if(longer.indexOf(subString)>=0){
targetString = subString;
return targetString;
}
}
}
}
find("instritesting","string");
console.log(targetString);