您的位置:首页 > 理论基础 > 数据结构算法

密码验证合格程序

2017-03-09 08:58 288 查看
要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有相同长度超2的子串重复


输入描述:

如果符合要求输出:OK,否则输出NG


输入例子:

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

输出例子:
OK
NG
NG
OK
AC代码:
import java.util.*;
public class Main {
// 1.长度超过8位
public static boolean checkLength(String str){
if(str.length() <= 8 ){
return false;
}
return true;
}
// 2.包括大小写字母.数字.其它符号,以上四种至少三种
public static boolean checkCharKinds(String str){
int low = 0,upper = 0,num = 0,other = 0;
char[] ch = str.toCharArray();
for(int i = 0;i < ch.length;i++){
if(ch[i] >='a' && ch[i] <='z'){
low = 1;
continue;
}else if(ch[i] >='A' && ch[i] <='Z'){
upper = 1;
continue;
}else if(ch[i] >='0' && ch[i] <='9'){
num = 1;
continue;
}else {
other = 1;
continue;
}
}
int total  =  low + upper + num + other;
return total >= 3 ? true : false;
}
// 3.不能有相同长度超2的子串重复
public static boolean checkCharRepeat(String str){
for(int i = 0;i < str.length() - 2;i++){
String subStr = str.substring(i, i + 3);
if(str.substring(i + 1).contains(subStr)){
return false;
}
}
return true;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
if(checkLength(str) && checkCharKinds(str) && checkCharRepeat(str)){
System.out.println("OK");
}else{
System.out.println("NG");
}
}
}
}


输出例子:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 算法