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

密码验证合格程序

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

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
  const inputs = [];
  while ((line = await readline())) {
    inputs.push(line);
  }
  inputs.forEach((pwd) =>
    !isValid12(pwd) || !isValid3(pwd) ? console.log("NG") : console.log("OK")
  );
})();
// 包括大小写字母.数字.其它符号,以上四种至少三种
function isValid12(pwd) {
  if (pwd.length <= 8) return false;
  const isAZ = /[A-Z]/.test(pwd);
  const isaz = /[a-z]/.test(pwd);
  const isNum = /\d/.test(pwd);
  const isSpecial = /[^a-zA-Z0-9]/.test(pwd);
  // console.log(isAZ, isaz, isNum, isSpecial)
  return +isAZ + +isaz + +isNum + +isSpecial >= 3;
}
// 不能有长度大于 2 的包含公共元素的子串重复 (注:其他符号不含空格或换行)
function isValid3(pwd) {
  for (let i = 0; i <= pwd.length; i++) {
    // 长度大于 3 的子串有重复,等于 3 的也一定有重复,只需考虑长度为 3 的重复子串,无需双循环
    const subStr = pwd.substring(i, i + 3);
    const lastIndex = pwd.lastIndexOf(subStr);
    // 反查子串索引相差大于 2,说明有长度大于 2 的重复子串
    if (lastIndex - i > 2) {
      return false; // console.log(subStr, pwd.lastIndexOf(subStr), i);
    }
  }
  return true;
}

全部评论

相关推荐

01-21 20:09
武汉大学 Java
点赞 评论 收藏
分享
2025-12-15 14:25
云南大学 Java
lei22:入职可能会看学信网,最好别伪装,这个简历找实习肯定是够的,肯定会有收 28 届实习生的公司的,多投就行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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