HJ20 密码验证合格程序

310 阅读1分钟

链接

密码要求:

1.长度超过8位

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

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

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
function fn(str){
    let count = 0;
    str = str.trim()
    if(str.length <= 8){
        return 'NG'
    }
    if(/\d/.test(str)){
        count++;
    }
    if(/[a-z]/.test(str)){
        count++;
    }
    if(/[A-Z]/.test(str)){
        count++;
    }
    if(/[^\da-zA-Z]/.test(str)){
        count++;
    }
    if(count < 3){
        return 'NG'
    }
    let m = {}
    let checked = {}
    for(let i = 0; i < str.length; i++){       
        m[str[i]] = m[str[i]] ? m[str[i]] : [] 
        m[str[i]].push(i)
    }
    for(let i = 0; i < str.length; i++){
        if(m[str[i]] && m[str[i]].length >= 2 && !checked[str[i]]){
            if(findSubStr(m[str[i]], 1, m, str)){
                return 'NG'
            }else{
                checked[str[i]] = true
            }            
        }
    }
    return "OK"
}
function findSubStr(arrIndex, deep = 1, m, str){
    if(deep > 2){
        return true;
    }
    let tm = {}
    for(let i = 0; i < arrIndex.length; i++){
        const pos = arrIndex[i] + 1; 
        if(pos < str.length && m[str[pos]] && m[str[pos]].length >= 2){
            tm[str[pos]] = tm[str[pos]] ? tm[str[pos]] : []
            tm[str[pos]].push(pos)
        }
    }
    for(let k in tm){
        if(tm[k] && tm[k].length >= 2){
            if( findSubStr(tm[k], deep + 1, m, str)){
                return true;
            }          
        }
    }
    return false
}
rl.on('line', function (line) {
    console.log(fn(line));
});

def checkLegal(pswd):    
    if len(pswd) <= 8:return False        
    else:
        #最大重复子串长度2+
        sub = []
        for i in range(len(pswd)-2):
            sub.append(pswd[i:i+3])
        if len(set(sub)) < len(sub):return False
        #check type
        type_ = 0
        import re
        Upper = '[A-Z]'
        Lowwer = '[a-z]'
        num = '\d'
        chars = '[^A-Za-z0-9_]'
        patterns = [Upper, Lowwer, num, chars]
        for pattern in patterns:
            pw = re.search(pattern, pswd)
            if pw : type_ += 1
        return True if type_ >= 3 else False
while True:
    try:
        pswd = input()
        print('OK' if checkLegal(pswd) else 'NG')
    except:
        break