题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String line = sc.nextLine(); int length = line.length(); int lowerCase = 0; int upperCase = 0; int digit = 0; int others = 0; for (int i = 0; i < length; i++) { if (Character.isLowerCase(line.charAt(i))) { lowerCase = 1; } else if (Character.isUpperCase(line.charAt(i))) { upperCase = 1; } else if (Character.isDigit(line.charAt(i))) { digit = 1; } else { others = 1; } } if (length <= 8) { System.out.println("NG"); } else if (lowerCase + upperCase + digit + others < 3) { System.out.println("NG"); } else { boolean res = repeat(line); if (res) { System.out.println("OK"); } else { System.out.println("NG"); } } } } public static boolean repeat(String s) { int k = s.length() / 2; for (int i = 3; i <= k; i++) { for (int j = 0; j <= s.length() - i; j++) { String substring = s.substring(j, j + i); String replace = s.replace(substring, ""); if (replace.length() < s.length() - substring.length()) { return false; } } } return true; } }