最长公共子串
本文最后更新于:7 个月前
最长公共子串
function max(text1, text2) {
let str = [],r = 0,i = 0,j = 0,l = 0,maxStr = [],max = Number.MIN_SAFE_INTEGER;
for (i; i < text1.length; i++) {
j = r;
l = i;
for (j; j < text2.length; j++) {
if (text1[i] === text2[j]) {
!r && (r = j);
str.push(text1[i]);
i++;
} else {
i = l;
str.length && j--;
maxStr = max === Math.max(max, str.length) ? maxStr : str;
max = Math.max(max, str.length);
str = [];
}
}
i = l;r=0;
}
maxStr = max === Math.max(max, str.length) ? maxStr : str;
max = Math.max(max, str.length);
return { max: max, str: maxStr };
}
console.log(max("adcvsassss", "avdcvssadcvsdc"));
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!