题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.;
import java.util.regex.;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String code = sc.nextLine();
boolean isOk = checkIsOk(code);
if(isOk) System.out.println("OK");
else System.out.println("NG");
}
} //检查是否达到要求 public static boolean checkIsOk(String code){ boolean isOk = false; if(code.length()>8){ if(!isRepeated(code)){ char[] chars = code.toCharArray(); int numMode = 0; if(Pattern.compile("[a-z]").matcher(code).find()){ numMode++; } if(Pattern.compile("[A-Z]").matcher(code).find()){ numMode++; } if(Pattern.compile("[0-9]").matcher(code).find()){ numMode++; } if(Pattern.compile("[^a-zA-Z0-9]").matcher(code).find()){ numMode++; } if(numMode>=3) isOk=true; } } return isOk; } //检查是否有重复子串 public static boolean isRepeated(String code){ boolean isRepeated = false; Set<String> ss = new HashSet<String>(); for(int i =0; i<code.length()-3; i++){ if(!ss.add(code.substring(i,i+3))) isRepeated = true; } return isRepeated; }
}