描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 1 \le n \le 100 \1≤n≤100
分析
难在判断字符串有没有重复的子串
可以双指针遍历判断有没有重复,也可以直接调用String查询子串的方法。
import java.util.*;
public class Main{
public static void main(String[] args){
List<String> li = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
li.add(sc.nextLine());
}
process(li);
}
public static void process(List<String> li){
int up = 0, low = 0, digit = 0, other = 0;
for(String str : li){
int len = str.length();
if(len <= 8){
System.out.println("NG");
continue;
}
int a=0, b=0, c=0, d=0;
for(int i=0; i<str.length(); i++){
char ch = str.charAt(i);
if(ch>='A' && ch<='Z') a=1;
else if(ch>='a' && ch<='z') b=1;
else if(ch>='0' && ch<='9') c=1;
else d=1;
}
if(a+b+c+d<3) {
System.out.println("NG");
continue;
}
if(isRepetitive(str)){
System.out.println("NG");
continue;
}
System.out.println("OK");
}
}
public static boolean isRepetitive(String s){
int n = s.length();
for(int i = 0; i < n-2; i++){
int tmp = i;
for(int j = i + 3; j < n; j++){
if(s.charAt(i) != s.charAt(j)){
continue;
}
int count = 0;
while(j < n && s.charAt(i) == s.charAt(j)){
i++;
j++;
count++;
}
if(count > 2){
return true;
}
}
i = tmp;
}
return false;
}
}