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

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

import java.util.Scanner;

// 注意类名必须为 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 str = in.next();
            if (str.length() <= 8) {
                System.out.println("NG");
                break;
            }

            //包括大小写字母 数字,其他符号以上四种其中的三种
            boolean a = false;
            boolean A = false;
            boolean num = false;
            boolean other = false;
            int inCount = 0;
            StringBuffer buffer = new StringBuffer(str);
            for (int i = 0; i < buffer.length(); i++) {
                if (inCount >= 3) {
                    break;
                }
                if (buffer.charAt(i) >= 'a' && buffer.charAt(i) <= 'z' && !a) {
                    inCount++;
                    a = true;
                }
                if (buffer.charAt(i) >= 'A' && buffer.charAt(i) <= 'Z' && !A) {
                    inCount++;
                    A = true;
                }
                if (buffer.charAt(i) >= '0' && buffer.charAt(i) <= '9' && !num) {
                    inCount++;
                    num = true;
                }
                if (!(buffer.charAt(i) >= '0' && buffer.charAt(i) <= '9') &&
                        !(buffer.charAt(i) >= 'a' && buffer.charAt(i) <= 'z') &&
                        !(buffer.charAt(i) >= 'A' && buffer.charAt(i) <= 'Z') && !other) {
                    inCount++;
                    other = true;
                }


            }

            if (inCount < 3) {
                System.out.println("NG");
                break;
            }
            if (JudgeRepeat(str)) {
                System.out.println("NG");
            } else {
                System.out.println("OK");
            }
        }
    }

    ///判断不能有相同长度超2的子串重复(>=3) x4%H0$ZL8@
    //398h$720CD0h&7f9~A403mex~lu#$*0+0CD0
    public static boolean JudgeRepeat(String str) {
        //判断字串是否包含在字符串中
        for (int i = 0; i < str.length() - 2; i++) {
            if ((str.substring(0, i) + str.substring(i + 3)).contains(str.substring(i, i + 3))) {
                return true;
            }
        }
        return false;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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