题解 | #查找两个字符串a,b中的最长公共子串#

查找两个字符串a,b中的最长公共子串

http://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506

let s1 = readline();
let s2 = readline();
let left = 0;
right = 0;
let maxLen = 0;
let res = "";
let short = s1.length >= s2.length ? s2 : s1;
let long = s1.length >= s2.length ? s1 : s2;
while (right < short.length) {
  let subStr = short.substring(left, right + 1);
  while (!long.includes(subStr)) {
    left++;
    subStr = short.substring(left, right + 1);
  }
  if (right - left + 1 > maxLen) {
    maxLen = right - left + 1;
    res = subStr;
  }
  right++;
}
console.log(res);

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-25 19:15
点赞 评论 收藏
分享
下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
“校招”、“3-5年经验”
xiaolihuam...:逆向工程不是搞外挂的吗,好像现在大学生坐牢最多的就是诈骗罪和非法侵入计算机系统罪,发美金,还居家办公,就是怕被一锅端,
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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