题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input = null; while((input = reader.readLine()) != null){ if(input.length() < 8){ System.out.println("NG"); }else{ int c1 = 0, c2 = 0, c3 = 0, c4 = 0; char[] c = input.toCharArray(); for(int i = 0; i < c.length; i++){ if(c[i] >= 'a' && c[i] <= 'z'){ c1 = 1; } else if(c[i] >= 'A' && c[i] <= 'Z'){ c2 = 1; } else if(c[i] >= '0' && c[i] <= '9'){ c3 = 1; } else{ c4 = 1; } if(c1+c2+c3+c4 >= 3) break; } if(c1+c2+c3+c4 >= 3 && repeat(input)){ System.out.println("OK"); }else{ System.out.println("NG"); } } } } // 判断是否有重复字串 public static boolean repeat(String s){ int n1 = s.length(); int mid = s.length()/2; for(int len = 3; len <= mid; len++){ for(int i = 0; i <= n1-len; i++){ String s1 = s.substring(0, i); // 前半部分字符串 String s2 = s.substring(i, i+len); // 要判断的字符串 String s3 = s.substring(i+len); // 后半部分字符串 if(s1.contains(s2) || s3.contains(s2)){ // 判断是否包含s2这个子串 return false; // 有重复字符 } } } return true; } }#华为机考##华为机试#