密码要求:
1.长度超过8位
2.包括:大写字母/小写字母/数字/其它符号,以上四种至少三种
3.不能分割出两个相等的长度大于 2 的子串,例如 abcabc 可以分割出两个 abc,不合法,ababa 则无法分割出2个aba。
注:其他符号不含空格或换行
数据范围:输入的字符串长度满足
密码要求:
1.长度超过8位
2.包括:大写字母/小写字母/数字/其它符号,以上四种至少三种
一组字符串。
如果符合要求输出:OK,否则输出NG
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000 021Abc1111
OK NG NG OK OK
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { while ((line = await readline())) { // 长度不超过8 if (line.length <= 8) { console.log("NG"); return; } // 不能有长度大于2的包含公共元素的子串重复 const length = line.length const arr = [] for(let i = 0; i < length - 1; i++) { // 将字符串切割成长度3的数组 arr.push(line.substring(i, i+3)) } // 去重后对比是否有重复 const setArr = [...new Set(arr)] if(setArr.length !== arr.length) { console.log("NG"); return; } let num = false; // 数字 let big = false; // 大写字母 let small = false; // 小写字母 let other = false; // 其他符号 const bigReg = /[A-Z]/; const smallReg = /[a-z]/; for (let i = 0; i < line.length; i++) { const isNum = Number.isNaN(Number(line[i])); if (!isNum) { num = true; } else if (bigReg.test(line[i])) { big = true; } else if (smallReg.test(line[i])) { small = true; } else { other = true; } } let count = 0; if (num) count += 1; if (big) count += 1; if (small) count += 1; if (other) count += 1; console.log(count >= 3 ? 'OK' : 'NG') } })();
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { /** * 思路: * 1. 长度超过8,判断长度 * 2. 包含四种符号中的三种,检测字符串有多少种符号 * 3. 寻找字符串中所有的子串,检查有没有相同的长度大于2的子串 * -> 搜索长度从3开始,用Set查重 * -> 子串长度i范围:3 ~ (len - 3) */ let caB = /[A-Z]{1,}/ // 大写字母 let lab = /[a-z]{1,}/ let dig = /\d{1,}/ // 数字 let other = /[^a-zA-Z0-9\s\n]{1,}/ // 其它字符 while(line = await readline()){ let markNum = 0 let reMark = 'OK' if (line.length < 8) { reMark = 'NG' console.log(reMark) // 为了通过测例 continue } if (caB.test(line)) markNum++ if (lab.test(line)) markNum++ if (dig.test(line)) markNum++ if (other.test(line)) markNum++ if (markNum < 3) { reMark = 'NG' console.log(reMark) continue } let sliceSet = new Set() // 列举子串 for (let i = 3; i <= line.length - 3; i++) { for (let j = 0; j + i <= line.length; j++) { // 从头开始截子串,子串长度从3开始,不会超过len - 3 let conStr = line.slice(j, i+j) // slice(sIdx, eIdx) if (sliceSet.has(conStr)) reMark = 'NG' else sliceSet.add(conStr) } } console.log(reMark) } }()
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(true){ let str=await readline(); if(!str) break; if(str.length<=8){ console.log("NG"); continue; } let map={}; for(let i in str){ let num=str[i].charCodeAt(); if(num>=97&&num<=122) map.low_case_letters=true; else if(num>=65&&num<=90) map.up_case_letters=true; else if(num>=48&&num<=57) map.nums=true; else map.others=true; } if(Object.keys(map).length<3){ console.log("NG"); continue; } let flag=true; for(let i=0;i<str.length-3;i++){ let child=str.slice(i,i+3); let num1=str.indexOf(child),num2=str.lastIndexOf(child); if(num1!=num2){ flag=false; console.log("NG"); break; } } if(!flag) continue; console.log("OK"); } }()
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let linelist = []; rl.on('line', function (line) { linelist.push(line); }); rl.on('close', function (line) { for(let i =0;i<linelist.length;i++){ let el = linelist[i]; if(el.length < 8){ console.log('NG'); continue; } //由于不会复杂的正则,只能一步一步来了 let list = []; if(/[a-z]/.test(el)){ list.push(1); } if(/[A-Z]/.test(el)){ list.push(2); } if(/[0-9]/.test(el)){ list.push(3); } if(/[\W]/.test(el)){ list.push(4); } if(list.length >= 3){ if(fn(el)){ console.log('OK') }else { console.log('NG') } }else { console.log('NG') } } }); function fn(line){ for(let i=0;i<line.length-3;i++){ let str = line.substr(i,3); let sty = line.split(str); //不等于2表示被多次分割了,即有相同的子集 if(sty.length !== 2){ return false; } } return true; }
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const arr = []; rl.on('line', function (line) { arr.push(line); if (line === "") { rl.close(); } }); rl.on("close", function(){ arr.forEach((item) => { // Condition 1 if(item.length <= 8) { console.log("NG"); return; } // Condition 2 const dataInd = Number(/\d+/.test(item)); const lowerCaseInd = Number(/[a-z]+/.test(item)); const upperCaseInd = Number(/[A-Z]+/.test(item)); const otherCharInd = Number(/[^A-Za-z0-9\s]+/.test(item)); const sum = dataInd + lowerCaseInd + upperCaseInd + otherCharInd; if (sum < 3) { console.log("NG"); return; } // Condition 3 for (let i = 0; i < item.length; i++) { const subStr = item.slice(i, i + 3); const nextIndex = item.indexOf(subStr, i + 3); if (nextIndex !== -1) { console.log("NG"); return; } } console.log("OK"); return; }); });
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.on('line', function (line) { //长度不大于8或者有相同长度大于2的重复子串,则NG if(line.length<=8||/(.{3,}).*\1/.test(line)){ console.log("NG"); return; }else{ let count=0;//符合规则的计数器,小于3,则NG if(/[0-9]/.test(line)){ count++; } if(/[a-z]/.test(line)){ count++; } if(/[A-Z]/.test(line)){ count++; } if(/\W/.test(line)){ count++; } if(count<3){ console.log("NG"); return; }else{ // for(let i=0;i+2<line.length;i++){ // for(let j=0;j+2<line.length;j++){ // //i!=j时,判断每一个字符是否相等,相等则NG // if(i!=j&&line.charAt(i)==line.charAt(j) && line.charAt(i+1)==line.charAt(j+1) && line.charAt(i+2)==line.charAt(j+2)){ // console.log("NG"); // return; // } // } // } //相同长度大于2的重复子串 // if(/(.{3,}).*\1/.test(line)){ // console.log("NG"); // return; // } console.log("OK"); return; } } });
let line const getTypes = password => { let count = 0; /[0-9]/.test(password) && count++; /[a-z]/.test(password) && count++; /[A-Z]/.test(password) && count++; /[^A-Za-z0-9]/.test(password) && count++; return count; } const hasRepeat = password => { let arr = password.split('') for(let i = 0; i < password.length - 2; i++) { let subStr = arr.splice(i, 3, ' ').join('') if(arr.join('').includes(subStr)) return false arr = password.split('') } return true } while(line = readline()) { let length = line.length > 8 let type = getTypes(line) >= 3 let repeat = hasRepeat(line) let ans = (length && type && repeat) ? 'OK' : 'NG' print(ans) }
let pwd=''; while(pwd = readline()){ let result='NG'; if(pwd.length>8){ let type={ a:0, b:0, c:0, d:0 }; for(let ch of pwd){ let asc=ch.charCodeAt(); if('A'.charCodeAt()<=asc && asc<='Z'.charCodeAt()){ type.a++; }else if('a'.charCodeAt()<=asc && asc<='z'.charCodeAt()){ type.b++; }else if('0'.charCodeAt()<=asc && asc<='9'.charCodeAt()){ type.c++; }else{ type.d++; } } let count=0; Object.keys(type).map(key=>{ if(type[key]>0){ count++; } }) if(count>=3){ let flag='OK'; for(let i=0;i<pwd.length-3;i++){ for(let j=i+3;j<pwd.length;j++){ let str=pwd.substring(i,j); if(pwd.split(str).length>2){ flag="NG"; break; } } if(flag==='NG'){ break; } } result=flag; console.log(result); }else{ console.log(result); } }else{ console.log(result); } }
通过正则匹配得到结果
function validation(str) {
if(str.length <= 8) return 'NG'
if(str.match(/(.{3,})(?=.{3,}\1)/g)) {
return 'NG'
}
let count = 0
if(str.match(/\d+/g)) {
count++
}
if(str.match(/[a-z]+/g)) {
count++
}
if(str.match(/[A-Z]+/g)) {
count++
}
if(str.match(/[^a-zA-Z0-9]/g)) {
count++
}
if(!count) {
return 'NG'
}
return 'AC'
}
console.log(validation('aa67aaaAc7'),validation('aa67aaaA&^c7'),validation('abc67abcA&^c7'))
//1.长度超过8位 function checkLength(stringPass){ if(stringPass==null||stringPass.length<=8) { return false; }else{ return true; } } // 2.包括大小写字母.数字.其它符号,以上四种至少三种 function checkCharkinds(stringPass){ var digit=0,lowercase=0,uppercase=0,others=0; for(var i=0;i<stringPass.length;i++){ if(stringPass[i]>='0'&&stringPass[i]<='9'){ digit=1; continue; } else if(stringPass[i]>='a'&&stringPass[i]<='z'){ lowercase=1; continue; } else if(stringPass[i]>='A'&&stringPass[i]<='Z'){ uppercase=1; continue; } else{ others=1; continue; } } var total=digit+lowercase+uppercase+others; return total>=3?true:false; } // 3.不能有相同长度超2的子串重复 function checkRepeat(stringPass){ for(var i=0;i<stringPass.length-2;i++){ var substr1=stringPass.slice(i,i+3); if(stringPass.indexOf(substr1)!=stringPass.lastIndexOf(substr1)){ return false; } } return true; } var readline = require('readline').createInterface(process.stdin,process.stdout); readline.on('line',function(stringPass){ if(checkLength(stringPass)&&checkCharkinds(stringPass)&&checkRepeat(stringPass)){ console.log('OK'); }else{ console.log('NG'); } })
const readline = require('readline'); const rl =readline.createInterface(process.stdin,process.stdout); rl.on('line',function(s){ console.log(s.length>8&&/[^a-zA-Z0-9]/.test(s)+/[a-z]/.test(s)+/[A-Z]/.test(s)+/\d/.test(s)>2&&!/(...).*\1/.test(s)?"OK":"NG") })//蛋疼的输入输出,**的js版本
var readline = require("readline"); var rl = readline.createInterface(process.stdin, process.stdout); rl.on("line", function(str){ if(str.length <= 8){ console.log('NG'); }else{ var re = /(.{3,}).*\1/; var re1 = /[a-z]/; var re2 = /[A-Z]/; var re3 = /[1-9]/; var re4 = /[\W|_]/; var num = 0; if(re.test(str)){ console.log('NG'); }else{ if(re1.test(str)){num++;} if(re2.test(str)){num++;} if(re3.test(str)){num++;} if(re4.test(str)){num++;} if(num >= 3){ console.log('OK'); }else{ console.log('NG'); } } } rl.close(); })
nodejs version 正则,长度 ,重复子串判断var readline = require('readline').createInterface(process.stdin,process.stdout);