题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
输入例子:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出例子:
OK
NG
NG
OK
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String password = scan.nextLine();
passwordCheck(password);
}//endwhile
scan.close();
}
/**
* 密码验证程序
* */
private static void passwordCheck(String password){
int length = password.length();
//长度超过8位
if(password != null && length > 8){
if(passwordCharkinds(password) && substringRepeat(password)){
System.out.println("OK");
}else{
System.out.println("NG");
}
}else{
System.out.println("NG");
}
}
/**
* 验证密码字符种类是否大于三种
* */
private static boolean passwordCharkinds(String password){
boolean flag = false;
int length = password.length();
int lower_letter_flag = 0;
int upper_letter_flag = 0;
int number_flag = 0;
int others_flag = 0;
for(int i = 0 ; i < length ; i++){
if(password.charAt(i) >= 'a' && password.charAt(i) <= 'z'){
lower_letter_flag = 1;
}else if(password.charAt(i) >= 'A' && password.charAt(i) <= 'Z'){
upper_letter_flag = 1;
}else if(password.charAt(i) >= '0' && password.charAt(i) <= '9'){
number_flag = 1;
}else{
others_flag = 1;
}
}
int total_kinds = lower_letter_flag + upper_letter_flag + number_flag + others_flag;
if(total_kinds >= 3){
flag = true;
}
return flag;
}
/**
* 验证密码是否有相同长度超2的子串重复
* 长度超2,长度超过2的子串,最小3个相邻字符的子串
* 算法思想:
* 只要存在3个相邻字符的子串重复,就不符合要求
* */
private static boolean substringRepeat(String password){
int length = password.length();
for(int i = 0 ; i < length - 2 ; i++){
for(int j = i + 1 ; j < length - 2 ; j++){
if(password.substring(j).contains(password.substring(i , i + 3))){
//
System.out.println("存在超2子串:" + password.substring(i , i + 3));
return false;
}
}
}
//
System.out.println("不存在超2子串:" + true);
return true;
}
}