题目描述
密码要求:1.长度超过8位2.包括大小写字母.数字.其它符号,以上四种至少三种3.不能有相同长度超2的子串重复说明:长度超过2的子串
输入描述
一组或多组长度超过2的子符串。每组占一行
输出描述
如果符合要求输出:OK,否则输出NG
输入例子
021Abc9000021Abc9Abc1021ABC9000021$bc9000
输出例子
OKNGNGOK
算法实现
import java.util.Scanner;/** * * All Rights Reserved !!! */public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);// Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt")); StringBuilder builder = new StringBuilder(); while (scanner.hasNext()) { String input = scanner.nextLine(); System.out.println(passwordValidate(input)); } scanner.close(); } private static String passwordValidate(String input) { final String OK = "OK"; final String NG = "NG"; // 长度超过8 if (input == null || input.length() < 9) { return NG; } int[] kind = new int[4]; // 统计 for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); if (ch >= 'A' && ch <= 'Z') { kind[0] |= 1; } else if (ch >= 'a' && ch <= 'z') { kind[1] |= 1; } else if (ch >= '0' && ch <= '9') { kind[2] |= 1; } else { kind[3] |= 1; } } // 小于三种 if ((kind[0] + kind[1] + kind[2] + kind[3]) < 3) { return NG; } for (int i = 0; i < input.length() - 3; i++) { String s1 = input.substring(i, i + 3); String s2 = input.substring(i + 3, input.length()); if (s2.contains(s1)) { return NG; } } return OK; }}