题解 | #密码截取#

密码截取

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
    line = await readline();
    getValue()
}()

function isPs(value) {
    for (let i = 0; i < value.length / 2; i++) {
        if (value.charAt(i) !== value.charAt(value.length - 1 - i)) {
            return false
        }
    }
    return true;
}


let line;
let startIndex;
let endIndex;
let max = 0;

function getValue() {
    back(0);
    console.log(max)
}

function back(i) {
    if (typeof startIndex === 'number' && typeof endIndex === 'number') {
        const subS = line.substring(startIndex, endIndex);
        if (isPs(subS) && subS.length > max) {
            max = subS.length;
        }
        endIndex = void 0;
        return;
    }

    if (i > line.length) {
        return
    }

    if (typeof startIndex !== 'number') {
        startIndex = i;
        back(i + 1);
        startIndex = void 0;
        back(i + 1);
    }

    if (typeof startIndex === 'number') {
        endIndex = i;
        back(i + 1);
        back(i + 1);
    }

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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