题解 | #密码验证合格程序#
密码验证合格程序
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);
while (in.hasNextLine()) {
String pwd = in.nextLine();
if (pwd.length() > 8 && !isContainCommonString(pwd) && contain3Type(pwd)) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
static boolean contain3Type(String pwd) {
int number = 0;
int lowChar = 0;
int upperChar = 0;
int others = 0;
for (char c : pwd.toCharArray()) {
if (c >= '0' && c <= '9') {
number = 1;
} else if (c >= 'a' && c <= 'z') {
lowChar = 1;
} else if (c >= 'A' && c <= 'Z') {
upperChar = 1;
} else {
others = 1;
}
}
return number + lowChar + upperChar + others >= 3;
}
static boolean isContainCommonString(String str) {
for (int i = 0; i < str.length()-3; i++) {
String child = str.substring(i, i + 3);
if (str.substring(i + 2).contains(child)) {
return true;
}
}
return false;
}
}
