首页 > 试题广场 >

密码验证合格程序

[编程题]密码验证合格程序
  • 热度指数:404078 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)

数据范围:输入的字符串长度满足

输入描述:

一组字符串。



输出描述:

如果符合要求输出:OK,否则输出NG

示例1

输入

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出

OK
NG
NG
OK
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)
    }
}()

发表于 2023-12-13 15:42:31 回复(0)
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");
    }
}()

发表于 2022-11-11 20:49:04 回复(0)
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;
}

发表于 2022-04-22 17:31:41 回复(0)
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;
    });
});

发表于 2022-01-22 00:20:59 回复(0)
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;
        }
        
    }
});

发表于 2021-11-25 23:51:48 回复(0)
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)
}

编辑于 2021-07-08 11:54:24 回复(0)
while(line = readline()){
    if (checkLength(line) &&checkCharkinds(line) && checkRepeat(line)) {
                console.log('OK');
            } else {
                console.log('NG');
            }
}
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;
        }
发表于 2021-01-09 22:15:05 回复(0)
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);
    }
}

编辑于 2020-10-27 12:21:06 回复(0)
这题特么考输入输出还是算法。。。。。
发表于 2020-04-12 23:55:58 回复(0)

通过正则匹配得到结果

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'))
发表于 2018-08-11 14:08:06 回复(0)
//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');     }
})



发表于 2018-06-05 22:57:23 回复(0)
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版本

发表于 2017-06-06 11:01:01 回复(0)
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();
})
发表于 2016-10-19 23:44:10 回复(0)
nodejs   version
正则,长度 ,重复子串判断
var readline = require('readline').createInterface(process.stdin,process.stdout);
readline.on('line',function(ans){
    check(ans)?console.log("OK"):console.log("NG");
})
function check(ps){
    const regcap = /[A-Z]+/;
    const regchar = /[a-z]+/;
    const regnum = /[0-9]+/;
    const other = /[^A-Za-z0-9]+/;
    regarr = [regcap,regchar,regnum,other];
    var condition1 = ps.length - 8>0;
    var condition2 = regarr.map(function(i){return i.test(ps)}).reduce(function(i,j){ return i+j})>=3;
    var condition3 = (function(str){
        for(var i = 0;i<str.length-2;i++){
            var substr = str.slice(i,i+3);
            if(str.indexOf(substr)!=str.lastIndexOf(substr)){
                return false;
            }
        }
        return true;
    })(ps);
    return condition1&&condition2&&condition3;
}

发表于 2016-08-29 10:39:23 回复(1)

问题信息

难度:
18条回答 92708浏览

热门推荐

通过挑战的用户

查看代码