题解 | #公共子串计算#
公共子串计算
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);
