题解 | #查找两个字符串a,b中的最长公共子串#
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
// Write your code here
let lines = [];
while ((line = await readline())) {
lines.push(line);
if(lines.length===2){
getMax(lines);
}
}
})();
function getMax(lines) {
let short = lines[0];
let long = lines[1];
if (short.length > long.length) {
short = lines[1];
long = lines[0];
}
let maxstr = short[0];
let max = 1;
for (let i = 0; i < short.length; i++) {
for (let j = 0; j <= i; j++) {
let str = short.slice(j, i + 1);
if (long.includes(str)) {
let len = str.length;
if (len > max) {
max = len;
maxstr = str;
}
}
}
}
console.log(maxstr);
}


查看6道真题和解析