题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; import java.util.*; import java.util.regex.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String sinput = in.nextLine(); if (null == sinput || sinput.length() < 9) { System.out.println("NG"); continue; } Pattern p = Pattern.compile("[a-z]"); int count = 0; if (p.matcher(sinput).find()) { count++; } if (Pattern.compile("[A-Z]").matcher(sinput).find()) { count++; } if (Pattern.compile("\\d").matcher(sinput).find()) { count++; } if (Pattern.compile("[^a-zA-Z0-9]").matcher(sinput).find()) { count++; } if (count < 3) { System.out.println("NG"); continue; } //不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行) //校验是否有重复子串 Boolean f = false; for (int i = 0; i < sinput.length() - 3; i++) { String pipei = sinput.substring(i, i + 3); String shengyu = sinput.substring(i + 3, sinput.length()); if (shengyu.contains(pipei)) { System.out.println("NG"); f = true; break; } } if (f) { continue; } System.out.println("OK"); } } }