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

密码验证合格程序

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

import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { 
            String s = in.nextLine();
            System.out.println(f(s));
        }
    }

    public static String f(String s) {
        int n = s.length();
        if(n < 8) return "NG";
        int num = 0, lower = 0, upper = 0, other = 0;
        Set<String> set = new HashSet<>();
        for(int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if(48 <= c && c <= 57) {
                num = 1;
            } else if (65 <= c && c <= 90) {
                upper = 1;
            } else if (97 <= c && c <= 122) {
                lower = 1;
            } else {
                other = 1;
            }
            for(int j = i; j < n; j++) {
                String sub = s.substring(i, j+1);
                if(set.contains(sub) && sub.length() > 2) {
                    return "NG";
                } else {
                    set.add(sub);
                }
            }
        }
        return num + lower + upper + other > 2 ? "OK" : "NG"; 
    }
}

全部评论

相关推荐

zhiyog:哈哈哈,其实是津巴布韦币
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务