题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
String string = input.nextLine();
if (string.length() <= 8) { //先判断长度,不满足直接NG
System.out.println("NG");
} else {
//再判断 四含其三,用int判断
int count = 0;
if (string.matches(".*[A-Z].*")) { //大写
count = count + 1;
}
if (string.matches(".*[a-z].*")) { //小写
count = count + 1;
}
if (string.matches(".*[0-9].*")) { // 数字
count = count + 1;
}
if (string.matches(".*[^a-zA-Z0-9\\S].*")) { //特殊字符 \S任何空白字符
count = count + 1;
}
if (count >= 3) { // 满足四有三 再判断子串重复
boolean flag = true;
HashSet<String> set = new HashSet<>();
// 不能有长度大于2的不含公共元素的子串重复
for (int i = 0; i < string.length() - 3; i++) {
String sub = string.substring(i, i + 3);
if (set.add(sub)) { // set能添加成功表示不重复
continue; // 没重复继续加
} else {
flag = false; //有重复即 NG
break;
}
}
if (flag) {
System.out.println("OK");
} else {
System.out.println("NG");
}
} else { // 不满足 四有三 NG
System.out.println("NG");
}
}
}
}
}

OPPO公司福利 1202人发布