题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*;
import java.util.regex.*;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String input = in.nextLine();
boolean regex = checkRegex(input);
boolean duplicate = checkDuplicate(input);
if (input.length() > 8 && regex && duplicate) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
private static boolean checkDuplicate(String str) {
int dupIndex = str.length() / 2;
for (int i = 0; i <= dupIndex; i++) {
for (int j = i + 3; j <= dupIndex; j++) {
String substring = str.substring(i, j);
String otherString = str.substring(j);
if (otherString.contains(substring)) {
return false;
}
}
}
return true;
}
static boolean checkRegex(String input) {
int count = 0;
Pattern upperLetter = Pattern.compile("[A-Z]+");
Pattern lowerLetter = Pattern.compile("[a-z]+");
Pattern number = Pattern.compile("\\d+");
Pattern others = Pattern.compile("[^a-zA-Z0-9]");
if (upperLetter.matcher(input).find()) {
count++;
}
if (lowerLetter.matcher(input).find()) {
count++;
}
if (number.matcher(input).find()) {
count++;
}
if (others.matcher(input).find()) {
count++;
}
if (count < 3) {
return false;
}
return true;
}
}