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

密码验证合格程序

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

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String input = in.nextLine();
            if (input.length() <= 8) {
                System.out.println("NG");
                continue;
            }
            for (int i = 0; i < input.length() - 6; i++) {
                for (int j = i + 3; j < input.length() -3 ; j++) {
                    String compareStr = input.substring(i, j);
                    String rightStr = input.substring(j);
                    if (rightStr.contains(compareStr)) {
                        System.out.println("NG");
                        return;
                    }
                }
            }
            int count = 0;
            if (input.matches(".*[a-z].*")) {
                count++;
            }
            if (input.matches(".*[A-Z].*")) {
                count++;
            }
            if (input.matches(".*[0-9].*")) {
                count++;
            }
            if (input.matches(".*[^0-9a-zA-Z//s].*")) {
                count++;
            }


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

全部评论

相关推荐

07-22 11:07
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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