题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
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 // 方法1:暴力破解 //避坑: 默认的方法取不到值,不是空格输入的值,是两个readline获取的a,b字符串 let a = await readline(); let b = await readline(); // 让 a 是短的 if (a.length > b.length) { [a, b] = [b, a]; } var maxLen = 0; for (var i = 0; i < a.length; i++) { for (var j = i; j <= a.length; j++) { // substring 左包右不包 , 所以j<= a.lenght var str = a.substring(i, j); // 获取每一段字符串 if (b.indexOf(str) > -1 && j - i > maxLen) { maxLen = j - i; } } } console.log(maxLen); })();