题解 | #密码验证合格程序#
密码验证合格程序
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);
label:
while (in.hasNextLine()) {
String input = in.nextLine();
if (input.length() < 9) { // 长度超过8位
System.out.println("NG");
continue;
}
HashSet<String> set = new HashSet<>();
boolean hasLower = false;
boolean hasUpper = false;
boolean hasNumber = false;
boolean others = false;
// 包括大小写字母.数字.其它符号,以上四种至少三种
// 不能有长度大于2的包含公共元素的子串重复
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c <= 'Z' && c >= 'A') { // 大写字母
hasUpper = true;
} else if (c <= 'z' && c >= 'a') { // 小写字母
hasLower = true;
} else if (c <= '9' && c >= '0') { // 数字
hasNumber = true;
} else { // 其它字符
others = true;
}
if (i < input.length() - 2) {
String substring = input.substring(i, i + 3);
if (set.contains(substring)) {
System.out.println("NG");
continue label;
} else {
set.add(substring);
}
}
}
byte b = 0;
if (hasLower) b++;
if (hasUpper) b++;
if (hasNumber) b++;
if (others) b++;
if (b < 3) {
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
}
#暴力破解##无正则##O(1)(52821)#