题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String input = in.nextLine();
if (input.length() <= 8) {
System.out.println("NG");
continue;
}
for (int i = 0; i < input.length() - 6; i++) {
for (int j = i + 3; j < input.length() -3 ; j++) {
String compareStr = input.substring(i, j);
String rightStr = input.substring(j);
if(compareStr.length() > rightStr.length()){
break;
}
if (rightStr.contains(compareStr)) {
System.out.println("NG");
return;
}
}
}
int count = 0;
if (input.matches(".*[a-z].*")) {
count++;
}
if (input.matches(".*[A-Z].*")) {
count++;
}
if (input.matches(".*[0-9].*")) {
count++;
}
if (input.matches(".*[^0-9a-zA-Z//s].*")) {
count++;
}
if ( count >= 3) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
}

查看3道真题和解析