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

密码验证合格程序

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        while (in.hasNext()) {
            String s = in.nextLine();
            if (s.length() <= 8 || s.contains(" ") || !check(s)) {
                System.out.println("NG");
                continue;
            }

            int count = 0;

            if (s.matches(".*[A-Z].*")) count++;

            if (s.matches(".*[a-z].*")) count++;

            if (s.matches(".*[0-9].*")) count++;

            if (s.matches(".*[^a-zA-Z0-9].*")) count++;

            if (count >= 3) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }

    //不能有长度大于2的包含公共元素的子串重复
    public static boolean check(String s) {
        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 3; j < s.length() - 3; j++) {
                if (s.substring(i, i + 3).contains(s.substring(j, j + 3))) return false;
            }
        }

        return true;
    }
}

//不能有长度大于2的包含公共元素的子串重复

这一条其实一直不太懂

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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