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

密码验证合格程序

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

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            System.out.println(getResult(a));
        }
        in.close();
    }
    private static String getResult(String str) {
        if (str.length() <= 8) {
            return "NG";
        }
        int upper = 0;
        int low = 0;
        int num = 0;
        int other = 0;
        for (int i = 0; i < str.length(); i++) {
            if (Character.isUpperCase(str.charAt(i))) {
                upper = 1;
            } else if (Character.isLowerCase(str.charAt(i))) {
                low = 1;
            } else if (Character.isDigit(str.charAt(i))) {
                num = 1;
            } else {
                other = 1;
            }

        }
        if (upper + low + num + other < 3) {
            return "NG";
        }
        Set<String> set = new HashSet<>();
        for (int i = 0; i < str.length() - 2; i++) {
            set.add(str.substring(i, i + 3));
        }
        if (set.size() < str.length() - 2) {
            return "NG";
        }
        return "OK";
    }
}

全部评论

相关推荐

鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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