题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String s = in.nextLine();
System.out.println(f(s));
}
}
public static String f(String s) {
int n = s.length();
if(n < 8) return "NG";
int num = 0, lower = 0, upper = 0, other = 0;
Set<String> set = new HashSet<>();
for(int i = 0; i < n; i++) {
char c = s.charAt(i);
if(48 <= c && c <= 57) {
num = 1;
} else if (65 <= c && c <= 90) {
upper = 1;
} else if (97 <= c && c <= 122) {
lower = 1;
} else {
other = 1;
}
for(int j = i; j < n; j++) {
String sub = s.substring(i, j+1);
if(set.contains(sub) && sub.length() > 2) {
return "NG";
} else {
set.add(sub);
}
}
}
return num + lower + upper + other > 2 ? "OK" : "NG";
}
}
查看17道真题和解析