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

密码验证合格程序

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String code = in.next();

//first , we determin the length,then we go the substring part, last, we determin if there have enough type of characters.
            if (code.length() <= 8) {

                System.out.println("NG");
                
            } else if (!subStringChecker(code)) {
                System.out.println("NG");
                
            } else if (!codeChecker(code)) {
                System.out.println("NG");
                
            }else{
                System.out.println("OK");
			}
        }
    }
    //判断字符数量格式是否符合要求。
    public static boolean codeChecker(String part) {
	  //We use hashset to stroe the characters that have appreared,because it helps us automaticlly repove duplicates.
        HashSet<String> checker = new HashSet<>();
        for (int i = 0; i < part.length(); i++) {
            if (Character.isDigit(part.charAt(i))) {
                checker.add("Digit");
            } else if (Character.isLowerCase(part.charAt(i))) {
                checker.add("LowerCase");
            } else if (Character.isUpperCase(part.charAt(i))) {
                checker.add("UpperCase");
            } else {
                checker.add("Other");
            }

        }
        return checker.size() >= 3;
    }
//we use sliding window to determin if there are repeated sustrings.
    public static boolean subStringChecker(String part) {
        for (int i = 0; i < part.length() - 2; i++) {
//Be careful, in the code snipped"substring(i,i+n)",the index of i+n is exclude, which means that the substring's elements will be part[i],part[i+1] and part[i+2].
            int indicator =  part.indexOf(part.substring(i, i + 3), i + 1);
            if (indicator != -1) {
                return false;
            }
        }
        return true;

    }
}

全部评论

相关推荐

09-25 16:00
门头沟学院 Java
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
珩珺:那些经历都太大太空了,实习的情况不了解,大创项目连名字、背景、目的及意义都没体现出来;地摊经济更是看完连卖的什么产品都不知道,项目成果直接写营收多少都更直观真实一点;后面那个校文体部的更是工作内容是组织活动整理流程,成果变成了当志愿者,而且你们学校本科学生会大一入学就直接当部长吗,志愿里面还提到了疫情防控,全面解封是22年12月的事情,可能时间上也有冲突。可能你花了钱人家就用AI给你随便写了点内容改了一下,没什么体现个性化的点
点赞 评论 收藏
分享
面我面我面我_秋招版:不是戈门,干哪来了,这就是java嘛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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