正则表达式
import java.util.Scanner;
import java.util.regex.Pattern;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String passWord = in.nextLine();
System.out.println(checkePassWord(passWord) ? "OK" : "NG");
}
}
public static boolean checkePassWord(String str) {
if (str.length() <= 8) return false;
int count = 0;
Pattern patternUpperCase = Pattern.compile("[A-Z]");
if (patternUpperCase.matcher(str).find()) count = count + 1;
Pattern patternLowerCase = Pattern.compile("[a-z]");
if (patternLowerCase.matcher(str).find()) count = count + 1;
Pattern patternDigit = Pattern.compile("[0-9]");
if (patternDigit.matcher(str).find()) count = count + 1;
Pattern patternOther = Pattern.compile("[^a-zA-Z0-9]");
if (patternOther.matcher(str).find()) count = count + 1;
if (count < 3) return false;
for (int i = 0; i < str.length() - 2; i++) {
String shortStr = str.substring(i, i + 3);
String longStr = str.substring(i + 3);
if (longStr.contains(shortStr)) return false;
}
return true;
}
}