题目
题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度大于2的子串重复
输入描述:
一组或多组长度超过2的字符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
答案
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String string = scanner.nextLine();
int digit = 0;
int lowerCase = 0;
int upperCase = 0;
int others = 0;
char[] ch = string.toCharArray();
Set<String> set = new HashSet();
for(int i=0;i<ch.length-2;i++){
set.add(string.substring(i,i+3));
}
if(string.length()<=8||set.size()<string.length()-2){
System.out.println("NG");
}else{
for(int i=0;i<ch.length;i++){
if(ch[i]>='0'&&ch[i]<='9'){
digit = 1;
}else if(ch[i]>='a'&&ch[i]<='z'){
lowerCase = 1;
}else if(ch[i]>='A'&&ch[i]<='Z'){
upperCase = 1;
}else{
others = 1;
}
}
System.out.println(digit+lowerCase+upperCase+others>=3?"OK":"NG");
}
}
}
}
答案解析
1、判断字符范围可以直接使用char字符,因为char字符会自动转换成数字;
2、判断重复字符时,使用substring()函数,然后进行遍历,将子字符串存入到HashSet中判断有无重复
3、判断重复还可以使用String类的contains函数