题解 | #查找两个字符串a,b中的最长公共子串#
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
//笨办法 第一反应就是暴力破解 拆短的字符串,看长的有没有,记录最长的
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let arr = [];
rl.on("line", function (line) {
arr.push(line);
if (arr.length == 2) {
let firstList = [];
let sercodList = [];
if(arr[0].length< arr[1].length){
firstList = arr[0];
sercodList = arr[1];
}else{
firstList = arr[1];
sercodList = arr[0];
}
let max = 0;
let res = "";
for (let index in firstList) {
let endIndex = 0;
let firstindex = Number(index)
let str = firstList.substring(firstindex, firstindex + endIndex);
let firstLength = firstList.length;
//存在
while (new RegExp(str).test(sercodList) && endIndex <= firstLength) {
let lenStr = str.length;
if (lenStr > max) {
max = lenStr;
res = str;
}
endIndex++;
str = firstList.substring(firstindex, firstindex + endIndex);
}
}
console.log(res)
}
});