题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
模拟
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); String str = null; while ((str = br.readLine()) != null) { pw.println(judge(str) ? "OK" : "NG"); } pw.flush(); pw.close(); br.close(); } private static boolean judge(String str) { int n = str.length(); // 条件1:长度超过8位 if (n <= 8) { return false; } // 条件2:包括大小写字母.数字.其它符号,以上四种至少三种 int hasUpper = 0, hasLower = 0, hasDigit = 0, hasOther = 0; for (char c : str.toCharArray()) { if (Character.isUpperCase(c)) { hasUpper = 1; } else if (Character.isLowerCase(c)) { hasLower = 1; } else if (Character.isDigit(c)) { hasDigit = 1; } else { hasOther = 1; } } if (hasUpper + hasLower + hasDigit + hasOther < 3) { return false; } // 条件3:不能有长度大于2的包含公共元素的子串重复 for (int i = 0; i < n - 3; i++) { String pattern = str.substring(i, i + 3); if (str.lastIndexOf(pattern) != i) { return false; } } return true; } }