【JavaScript】华为机试_HJ20_密码验证合格程序

题目描述

你需要书写一个程序验证给定的密码是否合格。

合格的密码要求:

(1)长度不少于8位

(2)必须包含大写字母、小写字母、数字、特殊字符中的至少三种

(3)不能分割出两个独立的、长度大于2的连续子串,使得这两个子串完全相同

题目链接:https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841?tpId=37\&tqId=21243\&rp=1\&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37\&difficulty=undefined\&judgeStatus=undefined\&tags=\&title=

解答

第一次通过示例

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

void async function () {
    // Write your code here
    while(line = await readline()){
        let res = "OK";
        let count = 0;
        if (line.length < 8) {
            console.log("NG");
            continue;
        }
        count = line.match(/[A-Z]/) ? ++count : count;
        count = line.match(/[a-z]/) ? ++count : count;
        count = line.match(/[0-9]/) ? ++count : count;
        count = line.match(/[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]/) ? ++count : count;
        if (count < 3) {
            console.log("NG");
            continue;
        }
        for (let i = 0; i < line.length - 5; i++) {
            let substr = line.slice(i, i+3);
            if (i > 0 && line.slice(0, i).includes(substr)) {
                res = "NG";
                break;
            } else if (i+3 < line.length && line.slice(i+3).includes(substr)) {
                res = "NG";
                break;
            }
        }
        console.log(res);
    }
}()

优化

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

void async function () {
    // Write your code here
    while(line = await readline()){
        if (isPassword(line)) {
            console.log("OK");
        } else {
            console.log("NG");
        }
    }
}()

function isPassword (pwd) {
    if (!pwd || pwd.length < 8) return false;

    let matchCount = 0;
    const regs = [/\d/g, /[A-Z]/g, /[a-z]/g, /\W/g];
    regs.forEach( reg => matchCount = reg.test(pwd) ? ++matchCount : matchCount);
    if (matchCount < 3) return false;
    
    for (let i = 0; i < pwd.length - 5; i++) {
        const substr = pwd.slice(i, i+3);
        if (pwd.slice(i+3).includes(substr)) return false;
    }
    return true;
}
相关推荐
caimouse15 分钟前
reactos编码规范
c语言·开发语言
小雨下雨的雨2 小时前
井字棋AI机器人实现详解 - Minimax算法实战-鸿蒙PC Electron框架完成
前端·人工智能·算法·华为·electron·鸿蒙
xieliyu.4 小时前
Java算法精讲:双指针(三)
java·开发语言·算法
CryptoPP5 小时前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
ZC跨境爬虫5 小时前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript
凌云拓界6 小时前
文件管理:让AI安全操作你的电脑 ——CogitoAgent开发实战(三)
javascript·人工智能·架构·开源·node.js
凌云拓界6 小时前
联网能力:让AI看见更广阔的世界 ——CogitoAgent开发实战(四)
javascript·人工智能·架构·node.js·创业创新
阳区欠6 小时前
【LangChain】LLM基础介绍
开发语言·python·langchain
Jinkxs7 小时前
Java 跨域14-Java 与区块链(Hyperledger)集成
java·开发语言·区块链
不爱吃糖的程序媛7 小时前
鸿蒙服务卡片实战:为新华字典应用添加桌面快捷查询卡片
华为·harmonyos