题解 | #查找两个字符串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 inputArr = [];//存放输入的数据 rl.on('line', function(line){ inputArr.push(line); }).on('close', function(){ console.log(maxString(inputArr[0], inputArr[1]))//调用函数并输出 }) function maxString(a, b) { if (a.length < b.length) { [a, b] = [b, a]; } let res = ''; for(let l = 0; l < b.length; l++) { for (let r = l + 1; r <= b.length; r++) { if(r - l > res.length && a.indexOf(b.slice(l, r)) !== -1) { res = b.slice(l, r); } } } return res; }