标题:密码验证合格程序 | 时间限制:1秒 | 内存限制:32768K
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足1<=n<=100
输入描述:
一组字符串。
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出
OK
NG
NG
OK
package test;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while (scan.hasNextLine()){
if(hasFlag(scan.nextLine())){
System.out.println("OK");
}else{
System.out.println("NG");
}
}
}
public static boolean hasFlag(String str){
//长度超过8位
if(str.length()<=8){
return false;
}
int num = 0,up = 0,low = 0,sign = 0;
char[] arr = str.toCharArray();
for(int i=0;i<arr.length;i++){
if(arr[i]>='0' && arr[i]<='9'){
num = 1;
continue;
}else if(arr[i]>='A' && arr[i]<='Z'){
up = 1;
continue;
}else if(arr[i]>='a' && arr[i]<='z'){
low = 1;
continue;
}else if(arr[i] != ' ' && arr[i] != '\n'){
sign = 1;
continue;
}
}
//3种类型以上
if((num+up+low+sign)<3){
return false;
}
//长度大于2公共子串
for(int i=0;i<str.length()-2;i++){
String strSub = str.substring(i,i+3);
if(str.substring(i+1).contains(strSub)){
return false;
}
}
return true;
}
}