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

密码验证合格程序

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);
        String str;
        while (in.hasNext()) {
            str = in.next();
            //重点,该输出NG的情况要包含完,各种情况符合都为NG,走遍了之后没有输出ng,最后就是OK

            // 检查长度是否超过8位
            if (str.length() <= 8) {
                System.out.println("NG");
                continue;
            }

            // 检查是否包括大小写字母、数字、其它符号中的至少三种
            int countTypes = 0;
            if (str.matches(".*[0-9].*")) countTypes++;
            if (str.matches(".*[a-z].*")) countTypes++;
            if (str.matches(".*[A-Z].*")) countTypes++;
            if (str.matches(".*[^a-zA-Z0-9 ].*")) countTypes++; // 包括其他符号
            if (countTypes < 3) {
                System.out.println("NG");
                continue;
            }

            // 检查是否有长度大于2的公共子串重复
            boolean hasRepeatingSubstring = hasRepeatingSubstring(str, 3);
            if (hasRepeatingSubstring) {
                System.out.println("NG");
                continue;
            }

            // 如果所有检查都通过,则输出"OK"
            System.out.println("OK");
        }
        in.close();
    }

    // 辅助方法,检查字符串是否有长度大于等于指定长度的重复子串
   private static boolean hasRepeatingSubstring(String str, int minLength) {
    int len = str.length();
    //012012012
    for (int i = 0; i < len - minLength; i++) {//0-2
        for (int j = i + minLength; j <= len-minLength; j++) {//0-2
		  //这两种if判断都通过了,不过底下的可以少判断几次【substring(1,4)即取得1,2,3】
            // if (str.substring(i, i + minLength).equals(str.substring(j - minLength+1, j+1 ))) {
                if (str.substring(i, i + minLength).equals(str.substring(j , j+minLength ))) {
                //0,2   ---1,0【】j+minlength
                return true;
            }
        }
    }
    return false;
}
}

全部评论

相关推荐

09-29 00:03
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
bg27强双非本,目前在学习golang后端gin框架部分,在b站找了一个轮子项目敲了一下,技术栈是gin&nbsp;+&nbsp;gorm&nbsp;+&nbsp;mysql&nbsp;+&nbsp;redis。我目前的想法是这一个月学习408和go八股以及刷算法然后在12月找个寒假实习然后大三下开始准备考研。我是考研意愿比较强烈,想问一下我是应该all&nbsp;in其中一个方向吗,我感觉我实习对我考研来说也是没什么帮助的好像。
牛客28967172...:毕业工作,考研,考公是完全不同的方向。 99%的人拼尽全力也只能把一个做好(能做好都已经是佼佼者了,比如进进大厂,考985或者考公) 如果你确定要考研可以不用学任何就业技术框架,也不用实习经验,刷题背知识点就行,但注意必须考92院校起步,因为这个年代双非硕毕业后完全不如双非本(互联网行业),可以说双非硕在互联网就业完全是负收益
投递哔哩哔哩等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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