题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

import java.util.Scanner;

public class Main {

    static final String OK = "OK";
    static final String NG = "NG";

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            if (isLegalLength(str) && hasEnoughComplexity(str) && hasNoIllegalSubString(str)) {
                System.out.println(OK);
            } else {
                System.out.println(NG);
            }
        }

    }

    static boolean isLegalLength(String password) {
        return password.length() > 8;
    }
    
    static boolean containsRange(int c, int s, int e) {
        return s <= c && c <= e;
    }

    static boolean hasEnoughComplexity(String password) {
        int complexity = 0;
        for (int i = 0; i < password.length(); i++) {
            if (Integer.bitCount(complexity) > 2) {
                return true;
            }
            char c = password.charAt(i);
            boolean hasDigit = containsRange(c, '0', '9');
            boolean hasUpperLetter = containsRange(c, 'A', 'Z');
            boolean hasLowerLetter = containsRange(c, 'a', 'z');
            boolean hasOthers = !(hasDigit || hasUpperLetter || hasLowerLetter);
            if (hasDigit) {
                complexity |= 1;
            }
            if (hasUpperLetter) {
                complexity |= (1 << 2);
            }
            if (hasLowerLetter) {
                complexity |= (1 << 3);
            }
            if (hasOthers) {
                complexity |= (1 << 4);
            }
        }
        return false;
    }

    static boolean hasNoIllegalSubString(String password) {
        for (int i = 3; i < password.length(); i++) {
            if (password.substring(i).contains(password.substring(i - 3, i))) {
                return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

家人们,我现在真的好纠结。我是26届的,目前还没有实习过。我现在的情况是,想参加秋招,但是感觉自己的简历特别空,没有实习经历会不会秋招直接凉凉啊?可我又听说现在很多公司对26届实习生也不太感冒,说什么不确定性大。而且我最近在准备考公,时间上也有点冲突。要是把时间花在实习上,备考时间就少了。但要是不实习,又怕以后就业有问题😫有没有懂行的友友帮我分析分析:26届现在不实习,秋招找工作真的会很难吗?考公和实习该怎么平衡啊?如果现在不实习,考完公再去找实习还来得及吗?真的太焦虑了,希望大家能给我点建议🙏
小破站_程序员YT:我可能和大家的观点不一样。人的精力是有限的,不能既要还要。你又想实习又想考公最后又要秋招上岸,我觉得哪有那么多的选择。你如果想考上岸,那就全力以赴。如果想秋招上岸,就继续投实习,投没了,就继续准备秋招,秋招不行继续春招。别到最后,考公没上岸,觉得是花了时间浪费在找实习上了, 秋招没上岸,觉得是浪费时间准备考公去了。我是认为很难说可以去平衡 不喜勿喷,可以叫我删除
实习与准备秋招该如何平衡
点赞 评论 收藏
分享
喜欢飞来飞去的雪碧在...:可以试一试字节
点赞 评论 收藏
分享
Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务