题解 | HJ20#密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String str = scanner.nextLine(); //长度大于8 if (str.length() > 8) { if (!isSubMoreThan3(str) && isTypeRight(str)) { System.out.println("OK"); } else { System.out.println("NG"); } } else { System.out.println("NG"); } } } public static boolean isTypeRight(String str) { //包含三种以上才返回true char[] chars = str.toCharArray(); int count = 0; for (Character c : chars) { if (Character.isUpperCase(c)) { count++; } if (Character.isLowerCase(c)) { count++; } if (Character.isDigit(c)) { count++; } if (!Character.isLetterOrDigit(c)) { count++; } } if (count >= 3) { return true; } return false; } //共同子串长度大于3 public static boolean isSubMoreThan3(String str) { char[] chars = str.toCharArray(); for (int i = 0; i < chars.length; i++) { if (i + 3 < chars.length) { for (int j = i + 1; j < chars.length - 3; j++) { //共同子串>3则首字母与2字母与3字母都相同 398h$720CD0h&7f9~A403mex~lu#$*0+0CD0 if (str.substring(i, i + 3).equals(str.substring(j, j + 3))) { return true; } } } } return false; } }