题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

const lines: string[] = [];

rl.on("line", function (line: string) {
    lines.push(line);
});

rl.on("close", () => {
    lines.forEach((item) => console.log(isLegalPassword(item)));
});

const isLegalPassword = (password: string): string => {
    // 1.长度超过8位
    if (password.length <= 8) {
        return "NG";
    }

    // 2.包括大小写字母.数字.其它符号,以上四种至少三种
    if (/' '/.test(password)) {
        return "NG";
    }
    const types = [
        /[A-Z]/.test(password),
        /[a-z]/.test(password),
        /[0-9]/.test(password),
        password.replace(/[0-9a-zA-Z]/g, "").length > 0,
    ];
    if (types.filter((item) => item === true).length < 3) {
        return "NG";
    }

    // 3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
    const childrenStr: string[] = [];
    for (let i = 0; i < password.length; i++) {
        const childStr = password.substring(i, i + 3);
        if (childrenStr.includes(childStr)) {
            return "NG";
        } else {
            childrenStr.push(childStr);
        }
    }

    return "OK";
};

全部评论

相关推荐

yubullym:双非目前 0 正式 offer,打算继续实习到 1 月准备春招了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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