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

密码验证合格程序

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";
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-20 18:18
是不是意味着秋招就完蛋了
花不开柳成荫:如果你是Java,是的
点赞 评论 收藏
分享
Twilight_mu:经典我朋友XXXX起手,这是那种经典的不知道目前行情搁那儿胡编乱造瞎指导的中年人,不用理这种**
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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