题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String reg_number = ".*\\d+.*";
String reg_upper = ".*[A-Z]+.*";
String reg_lower = ".*[a-z]+.*";
String reg_symbol = ".*\\W+.*";
while (in.hasNext()) {
int cnt = 0;
String input = in.nextLine();
if (input.length() < 8) {
System.out.println("NG");
return;
}
if (input.matches(reg_number))
cnt++;
if (input.matches(reg_upper))
cnt++;
if (input.matches(reg_lower))
cnt++;
if (input.matches(reg_symbol))
cnt++;
if (cnt >= 3 && !isMatch(input))
System.out.println("OK");
else
System.out.println("NG");
}
}
// 是否有超过2位的重复子串
public static boolean isMatch(String input) {
if (input.length() < 6) {
return false;
}
for (int i = 0; i < input.length() - 3; i++) {
int temp = i; // 移动指针
for (int x = i + 3; x < input.length(); x++) {
if (input.charAt(temp) != input.charAt(x)) {
temp = i;
continue;
}
if (temp - i == 2)
return true; // 存在重复子串
temp++;
}
}
return false;
}
}