题解 | 公共子串计算

公共子串计算

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
    const startLine = String(await readline())
    const endLine = String(await readline())
    let result = 0
    let maxString = ''
    for(let index=0;index + result<startLine.length;index++){
        let canNext = true
        while(canNext){
            // 如果子串长度加角标已经超出了父串的长度,就直接退出,表示当前已经是最长的子串长度了。
            if(index + result>=startLine.length){
                canNext = false
                continue
            }

            // 当前子串为父串中当前角标,截取最大长度再加1的那段。
            const subString = startLine.slice(index,index+result+1)

            // 如果在另一个字符串中不包含当前子串,则说明不是公共子串,直接退出。
            if(!endLine.includes(subString)){
                canNext = false
                continue
            }
            // if(!new RegExp(subString,'i').test(endLine)){
            //     canNext = false
            //     continue
            // }

            // 如果在另一个字符串中包含当前子串,则说明最大长度还可以加1。
            result = result + 1
            maxString = subString
        }
    }
    console.log(result)
}()

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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