题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const lines: string[] = []; rl.on("line", function (line: string) { lines.push(line); }); rl.on("close", () => { lines.forEach((item) => console.log(isLegalPassword(item))); }); const isLegalPassword = (password: string): string => { // 1.长度超过8位 if (password.length <= 8) { return "NG"; } // 2.包括大小写字母.数字.其它符号,以上四种至少三种 if (/' '/.test(password)) { return "NG"; } const types = [ /[A-Z]/.test(password), /[a-z]/.test(password), /[0-9]/.test(password), password.replace(/[0-9a-zA-Z]/g, "").length > 0, ]; if (types.filter((item) => item === true).length < 3) { return "NG"; } // 3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行) const childrenStr: string[] = []; for (let i = 0; i < password.length; i++) { const childStr = password.substring(i, i + 3); if (childrenStr.includes(childStr)) { return "NG"; } else { childrenStr.push(childStr); } } return "OK"; };