题解 | 密码验证合格程序
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String pwd = in.nextLine(); if (pwd.length() <= 8) { System.out.println("NG"); continue; } else { int digit = 0; int bigLetter = 0; int smallLetter = 0; int other = 0; for (int i = 0; i < pwd.length(); i++) { char c = pwd.charAt(i); if (Character.isDigit(c)) { digit++; } else if (Character.isUpperCase(c)) { bigLetter++; } else if (Character.isLowerCase(c)) { smallLetter++; } else { other++; } } int zeroCnt = 0; if (digit == 0) { zeroCnt++; } if (bigLetter == 0) { zeroCnt++; } if (smallLetter == 0) { zeroCnt++; } if (other == 0) { zeroCnt++; } // 有2种以上字符类型为0,打印NG if (zeroCnt >= 2) { System.out.println("NG"); continue; } boolean ng = false; out: for (int i = 0; i < pwd.length(); i++) { int repeatLength = 3; for (int j = 0 + i; j < pwd.length() / 2; j++) { for (int k = 0 + i; k < pwd.length() / 2; k++) { if (k + repeatLength >= pwd.length()) { break; } String str = pwd.substring(k, k + repeatLength); //System.out.print(str + "->" + pwd.replace(str, "")); // 替换掉截取的字符串后,如果替换后的字符串长度不止少了repeatLength,说明替换了多次,并且说明出现了重复的字符串 if (pwd.replace(str, "").length() < pwd.length() - repeatLength) { System.out.println("NG"); ng = true; break out; } //System.out.println(); } repeatLength++; } } if (!ng) { System.out.println("OK"); } } } } }