题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
输入例子:
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
输出例子:
OK NG NG OK
import java.util.Scanner; /** * 密码验证 * a-z:97-122 A-Z:65-90 0-9:48-57
*@Date 2016/9/20 * @author WGS */ public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String str2=sc.nextLine(); if(isValidString(str2)){ System.out.println("OK"); }else{ System.out.println("NG"); } } sc.close(); } private static boolean isValidString(String str) { //1 验证长度:长度超过8位 if(str== null ||str.length()<= 8){ return false; } // 2.包括大小写字母.数字.其它符号,以上四种至少三种 int typeCount=0; int[] type=new int[4]; for(int i=0;i<str.length();i++){ char c=str.charAt(i); int cNum=(int)c; if(cNum >=97 && cNum<= 122){ type[0]=1; }else if(cNum >=65 && cNum<=90){ type[1]=1; }else if(cNum >=48 && cNum<=57){ type[2]=1; }else{ type[3]=1; } } for(int i=0;i<type.length;i++){ typeCount+=type[i]; } if(typeCount<3) return false; //3不能有相同长度超2的子串重复 for(int i=0;i<str.length()-3;i++){ String s=str.substring(i, i+3); String tempStr=str.substring(i+3, str.length()); if(tempStr.contains(s)){ return false; } } return true; } }