题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            //长度超过8位
            //包括大小写字母.数字.其它符号,以上四种至少三种
            //重复性校验
            if ((a.length() > 8) && check(a) && copyCheck(a)) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }

    private static boolean copyCheck(String s) {
        int length = s.length();
        for (int i = 3; i <= length / 2; i++) {
            for (int j = 0; j < length - 2 * i; j++) {
                String sub = s.substring(j, j + i);
                if (s.indexOf(sub) != s.lastIndexOf(sub)) {
                    return false;
                }
            }
        }
        return true;
    }

    private static boolean check(String s) {
        int count = 0;
        if (s.matches(".*[0-9]+.*")) {
            count++;
        }
        if (s.matches(".*[A-Z]+.*")) {
            count++;
        }
        if (s.matches(".*[a-z]+.*")) {
            count++;
        }
        if (s.matches(".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"\\\\]+.*")) {
            count++;
        }
        boolean bool = count >= 3 ? true : false;
        return bool;
    }
}

解题思路:

1, 按照需求是对字符串进行三次校验;

2,分别是长度校验, 包含字符种类校验和重复字符校验;

3, 完成相关校验即可

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务