• 至少几种,分别判断是否包含。然后计数
  • 重复判断使用正则匹配组

import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * class com.sloera.nowcoder
 * user sloera
 * date 2022/2/27
 */
public class Main {
  public static void main(String[] args) {
    final Scanner in = new Scanner(System.in);
    while (in.hasNextLine()) {
      final String s = in.nextLine();
      boolean flag = isValid(s);
      if (flag) {
        System.out.println("OK");
      } else {
        System.out.println("NG");
      }
    }
  }

  private static boolean isValid(String s) {
    // 1.长度超过8位
    if (s.length() <= 8) {
      return false;
    }
    // 包括大小写字母.数字.其它符号,以上四种至少三种
    final Pattern lowerAlphabet = Pattern.compile(".*[a-z]+.*");
    final Pattern upperAlphabet = Pattern.compile(".*[A-Z]+.*");
    final Pattern number = Pattern.compile(".*[1-9]+.*");
    final Pattern other = Pattern.compile(".*[^0-9a-zA-Z]+.*");
    int count = 0;
    if (lowerAlphabet.matcher(s).matches()) {
      count++;
    }
    if (upperAlphabet.matcher(s).matches()) {
      count++;
    }
    if (number.matcher(s).matches()) {
      count++;
    }
    if (other.matcher(s).matches()) {
      count++;
    }
    if (count < 3) {
      return false;
    }
    // 不能有长度大于2的不含公共元素的子串重复
    if (Pattern.compile("^.*(.{3,}).*\\1.*$").matcher(s).find()) {
      return false;
    }
    return true;
  }
}