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

密码验证合格程序

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

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
         List <String> list =  new ArrayList();
        while (in.hasNextLine()) { // 注意 while 处理多个 case
              String str = in.nextLine();
              list.add(str);
        }
        for(String str:list){
             int i = 0;
		  //空格换行判断
            if(str.contains(" ")||str.contains("\n")){
                System.out.println("NG");
                continue;
            }
		  //长度判断
            if(str.length()<=8){
                System.out.println("NG");
                continue;
            }
		  //正则表达式判断分别判断是否有数字 大小写字母 其他字符 满足一个条件i加1
            if(str.matches(".*\\d+.*")){
                i++;
            }
            if(str.matches(".*[A-Z]+.*")){
                i++;
            }
            if(str.matches(".*[a-z]+.*")){
                i++;
            }
		  // “^”在方括号表达式中使用时,表示不接受该方括号表达式中的字符集合 判断是否含有其他字符
            if(str.matches(".*[^A-Za-z0-9\\s].*")){
                i++;
            }
		  //没有满足三个及以上条件
            if(i<3){
                 System.out.println("NG");
                continue;
            }
		  //调用判断重复子串方法
            if(repeated(str)){
                 System.out.println("NG");
                continue;
            }
            i = 0;
            System.out.println("OK");
        }
    }

    public static boolean repeated(String str){
	  //使用Set集合保存字串
             Set<String> set =new HashSet();
            for(int i=0;i<str.length()-2;i++){
                for(int j=i+3;j<str.length();j++){//双重循环把每一种字串切割出来作判断
                        String substr = str.substring(i,j);
                        if(set.contains(substr)){//判断集合中是否已经存在该字串
                            return true;
                        }
                        set.add(substr);//如果不存在则添加进集合
                }
            }
            return false;
    }
}

全部评论

相关推荐

起名字真难233:人家只有找猴子的预算,来个齐天大圣他们驾驭不住呀😂😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务