哇,这一题。。。真的就一步步去校验条件。重复子串那一块一直没有思路,知道看到了别的题解才发现,可以这么巧妙的实现,学到了。 实现方式如下show you code。。。

import java.util.*;

public class Main {


    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        String OK = "OK";
        String NG = "NG";
        List<String> results = new ArrayList<>();
        while (s.hasNextLine()){
            String password = s.nextLine();
            if (password.equals("")) break;
            // 长度超过8位,不含空格
            if (password.length() < 8 || password.split("\\s").length > 1){
                results.add(NG);
                continue;
            }
            // 检查重复子串
            int l = 0;
            int r = 3;
            boolean flag = false;
            for (;r < password.length();l++,r++){
                String sub = password.substring(l, r);
                if (password.substring(r).contains(sub)){
                    flag = true;
                }
            }
            if (flag){
                results.add(NG);
                continue;
            }

            int varieties = 0;// 种类计数
            String[] one = password.split("[a-z]");
            String[] two = password.split("[A-Z]");
            String[] three = password.split("[0-9]");
            String[] four = password.split("[^a-z|A-Z|0-9]");// 非空白符号
            if (one.length > 1 || (one.length == 1 && !password.equals(one[0]))){
                varieties += 1;
            }
            if (two.length > 1 || (two.length == 1 && !password.equals(two[0]))){
                varieties += 1;
            }
            if (three.length > 1 || (three.length == 1 && !password.equals(three[0]))){
                varieties += 1;
            }
            if (four.length > 1 || (four.length == 1 && !password.equals(four[0]))){
                varieties += 1;
            }
            if (varieties < 3){
                results.add(NG);
            }else if (varieties > 4){
                results.add(NG);
            }else{
                results.add(OK);
            }
        }
        for (String result : results) {
            System.out.println(result);
        }

    }
}