描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 1≤�≤100 1≤n≤100
输入描述:
一组字符串。
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入:
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
复制输出:
OK NG NG OK
import java.util.*;
import java.util.regex.Pattern;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
//判断是否符合正则
public static boolean checkChar(String in){
int a = 0;
int b = 0;
int c = 0;
int d = 0;
Pattern pattern1 = Pattern.compile("\\d");
Pattern pattern2 = Pattern.compile("[a-z]");
Pattern pattern3 = Pattern.compile("[A-Z]");
Pattern pattern4 = Pattern.compile("[!@#$%^&*()-+]");
if(pattern1.matcher(in).find()){
a =1;
}
if(pattern2.matcher(in).find()){
b =1;
}
if(pattern3.matcher(in).find()){
c =1;
}
if(pattern4.matcher(in).find()){
d =1;
}
return a+b+c+d >= 3;
}
public static int checkRepeatStr(String in){
int e= 0;
//不能有长度大于2的包含公共函数的子串重复,说明长度大于等于3的子串就要开始检查了
for (int i = 3; i < in.length() ; i++) {
if(in.substring(i).contains(in.substring(i-3,i))){
e++;
}
}
return e;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String in = scanner.nextLine();
//长度校验
if(in.length() <= 8 ){
System.out.println("NG");
continue;
}
if(checkChar(in)){
if(checkRepeatStr(in) >= 1){
System.out.println("NG");
}else{
System.out.println("OK");
}
}else{
System.out.println("NG");
}
}
}
}
知识点:
1. 正则表达式的使用:Pattern pattern1 = Pattern.compile("\\d"); 和 pattern1.matcher(in).find()。
2. 如何判断是否存在重复的子字符串,这里的3可以根据题目要求改变,返回值只要大于1就说明有重复子串
public static int checkRepeatStr(String in){
int e= 0;
//不能有长度大于2的包含公共函数的子串重复,说明长度大于等于3的子串就要开始检查了
for (int i = 3; i < in.length() ; i++) {
if(in.substring(i).contains(in.substring(i-3,i))){
e++;
}
}
return e;
}