题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = bf.readLine()) != null) {
// 长度验证
if (s.length() <= 8 ) {
System.out.println("NG");
continue;
}
// 大小写字母.数字.其它符号出现类型数统计
String[] regexes = {"\\d", "[a-z]", "[A-Z]", "[^\\da-zA-Z]"};
int types = 0;
for (String re : regexes) {
Pattern p = Pattern.compile(re);
Matcher m = p.matcher(s);
if (m.find()) {
types += 1;
}
}
if (types < 3) {
System.out.println("NG");
continue;
}
//找到重复出现的、长度至少为3的相同子串
if (s.replaceAll("(.{3,})(?=.*\\1)", "").length() < s.length()) {
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
}
查看12道真题和解析