題目描述
密碼要求:
1.長度超過8位
2.包括大小寫字母.數字.其它符號,以上四種至少三種
3.不能有相同長度超2的子串重複
說明:長度超過2的子串
輸入描述
一組或多組長度超過2的子符串。每組佔一行
輸出描述
若是符合要求輸出:OK,不然輸出NG
輸入例子
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
輸出例子
OK
NG
NG
OK
算法實現
import java.util.Scanner;
/**
*
* All Rights Reserved !!!
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
StringBuilder builder = new StringBuilder();
while (scanner.hasNext()) {
String input = scanner.nextLine();
System.out.println(passwordValidate(input));
}
scanner.close();
}
private static String passwordValidate(String input) {
final String OK = "OK";
final String NG = "NG";
// 長度超過8
if (input == null || input.length() < 9) {
return NG;
}
int[] kind = new int[4];
// 統計
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
kind[0] |= 1;
} else if (ch >= 'a' && ch <= 'z') {
kind[1] |= 1;
} else if (ch >= '0' && ch <= '9') {
kind[2] |= 1;
} else {
kind[3] |= 1;
}
}
// 小於三種
if ((kind[0] + kind[1] + kind[2] + kind[3]) < 3) {
return NG;
}
for (int i = 0; i < input.length() - 3; i++) {
String s1 = input.substring(i, i + 3);
String s2 = input.substring(i + 3, input.length());
if (s2.contains(s1)) {
return NG;
}
}
return OK;
}
}