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

密码验证合格程序

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


/**
密码要求:

1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 8< n <= 100
*/
const regxA = /[A-Z]/;
const regxa = /[a-z]/;
const regxN = /[0-9]/;
let isFaith = "OK";
rl.on("line", function (line) {
    isFaith = "OK";
    let str = line.trim();
	let len = str.length;
    if (len > 8 && len <= 100) {
        let count = new Array(4).fill(0);
        for (let i = 0; i < len; i++) {
            let item = line[i];
            if (regxA.test(item)) {
                count[0] = 1;
            } else if (regxa.test(item)) {
                count[1] = 1;
            } else if (regxN.test(item)) {
                count[2] = 1;
            } else { // 特殊字符串
                count[3] = 1;
            }
            // 检查公共子串重复
            if (i < len - 3) {
                let str = line.substring(i, i + 3);
                if (line.indexOf(str) !== line.lastIndexOf(str)) {
                    isFaith = "NG";
                }
            }
        }
        let total = count.reduce((prev, next) => prev + next, 0);
        if (total < 3) { // 如果不是含有3中特殊字符
            isFaith = "NG";
        }
    } else {
        isFaith = "NG";
    }

    console.log(isFaith);
});

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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