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

密码验证合格程序

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);
        String reg_number = ".*\\d+.*";
        String reg_upper = ".*[A-Z]+.*";
        String reg_lower = ".*[a-z]+.*";
        String reg_symbol = ".*\\W+.*";
        while (in.hasNext()) {
            int cnt = 0;
            String input = in.nextLine();
            if (input.length() < 8) {
                System.out.println("NG");
                return;
            }
            if (input.matches(reg_number))
                cnt++;
            if (input.matches(reg_upper))
                cnt++;
            if (input.matches(reg_lower))
                cnt++;
            if (input.matches(reg_symbol))
                cnt++;
            if (cnt >= 3 && !isMatch(input))
                System.out.println("OK");
            else
                System.out.println("NG");
        }
    }
    // 是否有超过2位的重复子串
    public static boolean isMatch(String input) {
        if (input.length() < 6) {
            return false;
        }
        for (int i = 0; i < input.length() - 3; i++) {
            int temp = i;   // 移动指针
            for (int x = i + 3; x < input.length(); x++) {
                if (input.charAt(temp) != input.charAt(x)) {
                    temp = i;
                    continue;
                }
                if (temp - i == 2)
                    return true;    // 存在重复子串
                temp++;
            }
        }
        return false;
    }
}

全部评论

相关推荐

01-26 18:45
门头沟学院 Java
一天代码十万三:哥们实习再包一下吧,产出太笼统了,尽量体现业务
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务