题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { // 注意 while 处理多个 case String a = in.nextLine(); System.out.println(getResult(a)); } in.close(); } private static String getResult(String str) { if (str.length() <= 8) { return "NG"; } int upper = 0; int low = 0; int num = 0; int other = 0; for (int i = 0; i < str.length(); i++) { if (Character.isUpperCase(str.charAt(i))) { upper = 1; } else if (Character.isLowerCase(str.charAt(i))) { low = 1; } else if (Character.isDigit(str.charAt(i))) { num = 1; } else { other = 1; } } if (upper + low + num + other < 3) { return "NG"; } Set<String> set = new HashSet<>(); for (int i = 0; i < str.length() - 2; i++) { set.add(str.substring(i, i + 3)); } if (set.size() < str.length() - 2) { return "NG"; } return "OK"; } }