题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
ensurePass(in.nextLine());
}
}
public static void ensurePass(String ss){
boolean flag = true;
int f1 = 0,f2 = 0,f3 = 0,f4 = 0;
//判断是否满足条件1
if(ss.length() < 9){
System.out.println("NG");
return;
}
//判断是否满足条件2
for(char ch : ss.toCharArray()){
if(f1 + f2 + f3 + f4 >= 3){
break;
}
if(Character.isDigit(ch)){
f1=1;
}else if(ch >= 'a' && ch <= 'z'){
f2=1;
}else if(ch >= 'A' && ch <='Z'){
f3=1;
}else if(ch == ' ' || ch == '\n'){
flag = false;
break;
}else{
f4=1;
}
}
//判断是否满足条件3
flag = getString(ss,0,3);
if(flag){
System.out.println("OK");
}else{
System.out.println("NG");
}
}
// 校验是否有重复子串
private static boolean getString(String str, int l, int r) {
if (r >= str.length()) {
return true;
}
if (str.substring(r).contains(str.substring(l, r))) {
return false;
} else {
return getString(str,l+1,r+1);
}
}
}