题解 | #密码验证合格程序#
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);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String str = in.next();
if (str.length() <= 8) {
System.out.println("NG");
break;
}
//包括大小写字母 数字,其他符号以上四种其中的三种
boolean a = false;
boolean A = false;
boolean num = false;
boolean other = false;
int inCount = 0;
StringBuffer buffer = new StringBuffer(str);
for (int i = 0; i < buffer.length(); i++) {
if (inCount >= 3) {
break;
}
if (buffer.charAt(i) >= 'a' && buffer.charAt(i) <= 'z' && !a) {
inCount++;
a = true;
}
if (buffer.charAt(i) >= 'A' && buffer.charAt(i) <= 'Z' && !A) {
inCount++;
A = true;
}
if (buffer.charAt(i) >= '0' && buffer.charAt(i) <= '9' && !num) {
inCount++;
num = true;
}
if (!(buffer.charAt(i) >= '0' && buffer.charAt(i) <= '9') &&
!(buffer.charAt(i) >= 'a' && buffer.charAt(i) <= 'z') &&
!(buffer.charAt(i) >= 'A' && buffer.charAt(i) <= 'Z') && !other) {
inCount++;
other = true;
}
}
if (inCount < 3) {
System.out.println("NG");
break;
}
if (JudgeRepeat(str)) {
System.out.println("NG");
} else {
System.out.println("OK");
}
}
}
///判断不能有相同长度超2的子串重复(>=3) x4%H0$ZL8@
//398h$720CD0h&7f9~A403mex~lu#$*0+0CD0
public static boolean JudgeRepeat(String str) {
//判断字串是否包含在字符串中
for (int i = 0; i < str.length() - 2; i++) {
if ((str.substring(0, i) + str.substring(i + 3)).contains(str.substring(i, i + 3))) {
return true;
}
}
return false;
}
}