题解 | 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;
    }
}

全部评论

相关推荐

06-26 15:35
武汉大学 运营
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 11:47
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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