题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
1、长度不够、有换行符,有空字符串 ,直接false
2.大小写数字其它符号四个规则四条正则,计数,小于3 false,其中其它符号,需要注意[^a-zA-Z/d]
3.长度大于2重复子串,利用obj的key值或者set去重长度3的子串,对比计数
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let arr = [];
rl.on("line", function (line) {
arr.push(line);
//一个一个的验证
let countRe = 0; // 验证2规则
let countStr = 0; //验证3字符串长度
//1.长度大于8
if (line.length > 8 && !line.includes("\n") && !line.includes(" ")) {
//大写小写字母
let re1 = /[a-z]/; //小写
let re2 = /[A-Z]/; //大写
let re3 = /\d/; //数字
// let re4 = /[^\u4e00-\u9fa5a-zA-Z\d,\.,。]+/; //除了空格、换行的其它字符
//let re4 = /[^\u4e00-\u9fa5a-zA-Z\d]/; //非汉字非大小写非数字 或者。 .
let re4 = /[^a-zA-Z\d]/; //非汉字非大小写非数字 或者。 .
if (line.match(re1)) {
countRe++;
}
if (line.match(re2)) {
countRe++;
}
if (line.match(re3)) {
countRe++;
}
if (line.match(re4)) {
countRe++;
}
if (countRe < 3) {
console.log("NG");
} else {
const obj = {};
for (let i = 0; i < line.length; i++) {
let sub = line.substring(i, i + 3);
if (sub.length < 3) {
continue;
}
obj[sub] = sub;
countStr++;
}
if (Object.keys(obj).length == countStr) {
console.log("OK");
} else {
console.log("NG");
}
}
} else {
console.log("NG");
}
});