题解 | #查找两个字符串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].lengtharr[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(firstindexfirstindex + 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(firstindexfirstindex + endIndex);
      }
    }
    console.log(res)
  }
});

全部评论

相关推荐

点赞 评论 收藏
分享
05-24 14:12
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务