题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
省去不必要的步骤
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
if(JudgeCode(str) == false){
System.out.println("NG");
}else{
System.out.println("OK");
}
}
}
public static boolean JudgeCode(String str){
int count_1 = 0, count_2 = 0, count_3 = 0, count_4 = 0;
char[] s = str.toCharArray();
for(int i = 0; i < s.length; i++){
if(Character.isDigit(s[i])){
count_1 = 1;
}else if(Character.isUpperCase(s[i])){
count_2 = 1;
}else if(Character.isLowerCase(s[i])){
count_3 = 1;
}else{
count_4 = 1;
}
}
int ans = count_1 + count_2 + count_3 + count_4;
boolean sol = true;
//判断密码是否有至少3种以上不同类型的输入
if(ans < 3){
sol = false;
}else{
//判断密码长度是否超过8位
if(s.length < 8){
sol = false;
}
//判断是否有子串重复
int left = 0, right = 3;
while (right < s.length){
if (str.substring(right).contains(str.substring(left, right))) {
sol = false;
}
left++;
right++;
}
}
return sol;
}
}