【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;
}
相关推荐
Marst Code5 小时前
(python)2026Plotly 库评估:交互式可视化到底值不值得引入?
开发语言·python
Miao121316 小时前
微服务 API 测试实践:海外某民宿平台如何构建模式驱动测试基础设施
java·开发语言
拥抱太阳06166 小时前
HarmonyOS 应用开发《掌上英语》第23篇-图标与颜色系统构建可主题化视觉体系
华为·harmonyos
张元清7 小时前
React useResizeObserver Hook:追踪元素尺寸变化(2026)
javascript·react.js
红烧大青虫7 小时前
HarmonyOS应用开发实战:小事记 - UIAbility 的冷启动/热启动/后台启动三种场景与 launchParam 解析
后端·华为·harmonyos·鸿蒙系统
CRMEB系统商城7 小时前
开源自建还是SaaS订阅?算一笔3年经济账
java·大数据·开发语言·开源
b130538100497 小时前
HarmonyOS应用开发实战:小事记 - 多级页面路由的 back 逻辑与参数回传模式
华为·harmonyos·鸿蒙系统
keyipatience8 小时前
线程栈与TLS和线程互斥
java·linux·服务器·开发语言·ubuntu
花开彼岸天~8 小时前
鸿蒙原生开发手记:徒步迹 - 自定义组件开发规范
后端·华为·harmonyos·鸿蒙系统
我的xiaodoujiao8 小时前
快速学习Python基础知识详细图文教程9--函数进阶
开发语言·python·学习·测试工具