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

import java.util.*;

// 注意类名必须为 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 line = in.nextLine();
            int count = 0;
            if(line.length()<=8){
                System.out.println("NG");
                continue;
            }
            if(line.matches(".*[0-9]+.*")){
                count++;
            }

            if(line.matches(".*[a-z]+.*")){
                count++;
            }

            if(line.matches(".*[A-Z]+.*")){
                count++;
            }

            if(line.matches(".*[^\\w\\s]+.*")){
                count++;
            }
            
            if(count>=3 && !isContainsSubstr(line,0,3)){
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }
    public static boolean isContainsSubstr(String str,int l, int r){
        if(r>=str.length()){
            return false;
        }
        if(str.substring(r).contains(str.substring(l,r))){
            return true;
        } else{
            return isContainsSubstr(str,l+1,r+1);
        }
    }
}

全部评论

相关推荐

码农索隆:想看offer细节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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