题解 | #密码截取#

密码截取

https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

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
    // while(line = await readline()){
    //     let tokens = line.split(' ');
    //     let a = parseInt(tokens[0]);
    //     let b = parseInt(tokens[1]);
    //     console.log(a + b);
    // }
    let line = await readline();
    // dp[i][j]=s[i]==s[j]&&dp[i+1][j-1]
    // let dp = new Array(line.length).fill(0).map(() => {
    //     return new Array(line.length).fill(false);
    // });
    let memo = {};
    // console.log(line.length)
    let maxlen = 1;
    for (let i = line.length - 1; i >= 0; i--) {
        for (let j = i + 1; j < line.length; j++) {
            if (line[i] == line[j]) {
                let key = `${i}-${j}`;
                if (j - i + 1 <= 3) {
                    memo[key] = true;
                } else {
                    let prevKey = `${i + 1}-${j - 1}`;
                    memo[key] = memo[prevKey];
                }
                if (memo[key] ) {
                    maxlen = Math.max(maxlen, j - i + 1);
                }
            }
        }
    }
    console.log(maxlen);
})();

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务