题解 | 密码验证合格程序

密码验证合格程序

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

import java.util.*;
import java.util.regex.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.next();
            if (str.length() <= 8) {
                System.out.println("NG");
                continue;
            }
            if (!match(str)) {
                System.out.println("NG");
                continue;

            }
            if (getstring(str, 0, 3)) {
                System.out.println("NG");
                continue;

            }
            System.out.println("OK");
        }
    }

    public static boolean match(String str) {
        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(str).find()) {//分别看能不能匹配到这种模式
            count++;
        }
        if (p2.matcher(str).find()) {
            count++;
        }
        if (p3.matcher(str).find()) {
            count++;
        }
        if (p4.matcher(str).find()) {
            count++;
        }
        return count >= 3;
    }
    public static boolean getstring(String str, int l, int r) {


        if (str.substring(r).contains(str.substring(l, r))) {
            return true;
        } else if (r + 1 < str.length()) {
            return getstring(str, l + 1, r + 1);//递归判断大于二长度的的重复子串是否存在
        }

        return false;



    }
}

全部评论

相关推荐

顺利毕业的鸽子:这个不一定,找hr跟进一下
点赞 评论 收藏
分享
接好运Plus:定时器项目都被用烂了,感觉
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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