题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.regex.; import java.util.; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s = sc.nextLine(); //1.满足长度超过8位 if(s.length() <= 8){ System.out.println("NG"); continue; } if(getMatch(s)){ System.out.println("NG"); continue; } //2.不能有长度大于2的不含公共元素的子串重复 if(getString(s, 0, 3)){ System.out.println("NG"); continue; } System.out.println("OK"); } } //3.检查是否有存在大于3的重复子串 private static boolean getString(String s, int l, int r){ if(r >= s.length()){ return false; } if(s.substring(r).contains(s.substring(l, r))){ return true; } else { return getString(s, l + 1, r + 1); } } //4.检查是否满足正则 private static boolean getMatch(String s){ int count = 0; Pattern p1 = Pattern.compile("[A-Z]"); if(p1.matcher(s).find()){ count++; } Pattern p2 = Pattern.compile("[a-z]"); if(p2.matcher(s).find()){ count++; } Pattern p3 = Pattern.compile("[0-9]"); if(p3.matcher(s).find()){ count++; } //非匹配字母或者数字或者下划线或者汉字 Pattern p4 = Pattern.compile("[^a-zA-Z0-9]"); if(p4.matcher(s).find()){ count++; } if(count >= 3){ return false; } else { return true; } } }