题目要求

密码要求:       1.长度超过8位       2.包括大小写字母.数字.其它符号,以上四种至少三种       3.不能有相同长度超2的子串重复       说明:长度超过2的子串

输入例子:
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000

 

输出例子:
OK NG NG OK

 

 

 

import java.util.Scanner;
 
/**
 * Author: 王俊超
 * Date: 2015/12/22 15:45
 * 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++) {           //因为超过2即最小是三
            String s1 = input.substring(i, i + 3);              // 吧字符串分隔,看后面的是否包含前面的即可
            String s2 = input.substring(i + 3, input.length());
            if (s2.contains(s1)) {
                return NG;
            }
        }
        return OK;
    }
}
 
 
字符串截取,substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。       //即subString是左闭右开的
beginIndex - 起始索引(包括)。从0开始
endIndex - 结束索引(不包括)。
"unhappy".substring(2) returns "happy"
"hamburger".substring(4, 8) returns "urge"
posted on 2015-12-24 14:24  小飞虫子  阅读(233)  评论(0编辑  收藏  举报