【问题描述】
开发一个密码检查软件,密码要求:
-
长度超过8位
-
包括大小写字母.数字.其它符号,以上四种至少三种
-
不能有相同长度大于或等于2的子串重复
【输入形式】
一组或多组长度超过2的子符串。每组占一行
【输出形式】
如果符合要求输出:OK,否则输出NG
【样例输入】
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
【样例输出】
OK
NG
NG
OK
import java.util.Scanner;
public class p6 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
String str = s.next();
int length = str.length();
//1.长度超过8位
if (length <= 8) {
System.out.println("NG");
continue;
}
//2.包括大小写字母.数字.其它符号;以上四种至少三种
char[] chars = str.toCharArray();
int count1 = 0;
int count2 = 0;
int count0 = 0;
int count = 0;
for (int i = 0; i < length; i++) {
if (Character.isDigit(chars[i])) count0 = 1;
else if (Character.isUpperCase(chars[i])) count2 = 1;
else if (Character.isLowerCase(chars[i])) count1 = 1;
else count = 1;
}
if (count0 + count2 + count1 + count < 3) {
System.out.println("NG");
continue;
}
//3.不能有相同长度大于2的子串重复
String result = "OK";
for (int i = 0; i <= str.length()-2; i++) {
for (int j = i + 2; j <= str.length(); j++) {
String substring = str.substring(i, j);
// System.out.println(substring+" "+str.indexOf(substring));
if (str.indexOf(substring)!=i&&(j-str.indexOf(substring))>=(substring.length()*2)) {
result = "NG";
}
}
}
System.out.println(result);
}
}
}