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

密码验证合格程序

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;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:04
毕业生招你惹你了,问一个发薪日来一句别看网上乱七八糟的你看哪个工作没有固定发薪日扭头就取消了面试就问了一句公司都是这个态度吗还搞上人身攻击了...
程序员小白条:呃呃呃,都还没面试,我都不会问这么细,何况通不通过,去不去都另说,你没实力和学历的话,在外面就这样,说实话没直接已读不回就不错了,浪费时间基本上
点赞 评论 收藏
分享
身边有人上海、深圳&nbsp;6、7k&nbsp;都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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