题解 | 密码验证合格程序
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*;
import java.util.regex.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String str = in.next();
if (str.length() <= 8) {
System.out.println("NG");
continue;
}
if (!match(str)) {
System.out.println("NG");
continue;
}
if (getstring(str, 0, 3)) {
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
public static boolean match(String str) {
int count = 0;
Pattern p1 = Pattern.compile("[A-Z]"); //定义一种正则模式
Pattern p2 = Pattern.compile("[a-z]");
Pattern p3 = Pattern.compile("[0-9]");
Pattern p4 = Pattern.compile("[^A-Za-z0-9]");
if (p1.matcher(str).find()) {//分别看能不能匹配到这种模式
count++;
}
if (p2.matcher(str).find()) {
count++;
}
if (p3.matcher(str).find()) {
count++;
}
if (p4.matcher(str).find()) {
count++;
}
return count >= 3;
}
public static boolean getstring(String str, int l, int r) {
if (str.substring(r).contains(str.substring(l, r))) {
return true;
} else if (r + 1 < str.length()) {
return getstring(str, l + 1, r + 1);//递归判断大于二长度的的重复子串是否存在
}
return false;
}
}