题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; import java.util.regex.Pattern; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 // while (in.hasNextInt()) { // 注意 while 处理多个 case // int a = in.nextInt(); // int b = in.nextInt(); // System.out.println(a + b); // } String line = in.nextLine(); int len = line.length(); if (len < 9) { System.out.print("NG"); return; } Pattern lowerPattern = Pattern.compile("[a-z]"); Pattern upperPattern = Pattern.compile("[A-Z]"); Pattern numberPattern = Pattern.compile("[0-9]"); Pattern otherPattern = Pattern.compile("[^a-zA-Z0-9]"); Pattern enterPattern = Pattern.compile("\\r"); Pattern linePattern = Pattern.compile("\\n"); if (linePattern.matcher(line).find() || enterPattern.matcher(line).find()) { System.out.print("NG"); return; } //判断重复子串 for (int i = 0; i < line.length() - 3; i++) { String subStr = line.substring(i, i + 3); String subLine = line.substring(i + 1); if (subLine.contains(subStr)) { System.out.print("NG"); return; } } int count = 0; if (lowerPattern.matcher(line).find()) { count++; } if (upperPattern.matcher(line).find()) { count++; } if (numberPattern.matcher(line).find()) { count++; } if (otherPattern.matcher(line).find()) { count++; } if (count == 3 || count == 4) { System.out.print("OK"); } else { System.out.print("NG"); } } }