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

密码验证合格程序

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 s = in.next();
 
            Set<String> commonSubstrings = new HashSet<>();
            int CapLetters = 0, LowLetters = 0, numbers = 0, others = 0;
            boolean hasDuplicates = false;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                // 检查大小写字母,数字和其它符号
                if ('A' <= c && c <= 'Z')  CapLetters = 1;
                else if ('a' <= c && c <= 'z')  LowLetters = 1;
                else if ('0' <= c && c <= '9')  numbers = 1;
                else if (c != ' ' && c != '\n')  others = 1;  
                // 检查当前string里是否有长度大于2的substring
                if (i > 1) {
                    if (!commonSubstrings.add(s.substring(i - 2, i + 1))) {
                        hasDuplicates = true;
                        break;
                    }
                }
            }
            // 密码要求:
            // 1.长度超过8位
            // 2.包括大小写字母.数字.其它符号,以上四种至少三种
            // 3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
            if (s.length() < 9 || hasDuplicates || (CapLetters + LowLetters + numbers + others < 3))
                System.out.println("NG");
            else 
                System.out.println("OK");
        }
    }
}

全部评论

相关推荐

08-05 14:06
门头沟学院 Java
驼瑞驰_招募评论官版...:哈哈哈,hr怪实诚的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-29 14:02
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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