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

密码验证合格程序

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

import java.util.*;
import java.util.regex.Pattern;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.next();
            if (s.length() < 8) {
                System.out.println("NG");
                continue;
            }
            if (isRepeat(s, 0, 3)) {
                System.out.println("NG");
                continue;
            }
            if (isMatch(s)) {
                System.out.println("NG");
                continue;
            }
            System.out.println("OK");

        }
    }

    public static boolean isRepeat(String s, int start, int end) {
        if (end >= s.length())
            return false;
        if (s.substring(end).contains(s.substring(start, end)))
            return true;
        else
            return  isRepeat(s, start + 1, end + 1);
    }
    public static boolean isMatch(String s) {
        int count = 0;
        Pattern p1 = Pattern.compile("[A-Z]");
        Pattern p2 = Pattern.compile("[a-z]");
        Pattern p3 = Pattern.compile("[0-9]");
        Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");
        if (p1.matcher(s).find()) {
            count++;
        }
        if (p2.matcher(s).find()) {
            count++;
        }
        if (p3.matcher(s).find()) {
            count++;
        }
        if (p4.matcher(s).find()) {
            count++;
        }
        if (count > 2)
            return false;
        return true;

    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
05-01 13:13
ecece:这么明目张胆虚报就业率啊
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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