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

密码验证合格程序

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

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

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> list=new ArrayList<>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            list.add(line);
        }
        for (String s : list) {
            if(s.length()<=8){
                System.out.println("NG");
            } else{
                if(isLegal(s)&&isInnerStringNotRepeat(s))
                    System.out.println("OK");
                else
                    System.out.println("NG");
            }
        }
    }

    public static boolean isLegal(String s) {
        int[] count=new int[4];
        for (int i = 0; i < s.length(); i++){
            if(s.charAt(i) <= '9' && s.charAt(i) >= '0'){
                count[0]=1;
            } else if(s.charAt(i)<= 'Z' && s.charAt(i) >= 'A'){
                count[1]=1;
            } else if(s.charAt(i)<= 'z' && s.charAt(i) >= 'a'){
                count[2]=1;
            } else {
                count[3]=1;
            }
        }
        return count[0]+count[1]+count[2]+count[3]>=3;
    }

    private static boolean isInnerStringNotRepeat(String s) {
        for (int i = 0; i < s.length()-3; i++){
            String subStr = s.substring(i, i+3);
            for (int j = i+1; j < s.length()-3; j++){
                String subStr2 = s.substring(j, j+3);
                if(subStr2.equals(subStr)) return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

叁六玖:你看,最后不是让你加油,就是鼓励你,还祝福你求职顺利。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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