题解 | 密码验证合格程序

密码验证合格程序

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()) {
            String secret = in.next();

            if (secret.length() < 8) {
                System.out.println("NG");
                continue;
            }

            //大写,小写,数字,特殊字符
            byte[] rule2 = new byte[4];

            boolean repeated = false;
            for (int i = 0; i < secret.length(); i++) {
                repeated = isChildStr(secret, i, i+3);
                if (repeated) {
                    break;
                }
                char c = secret.charAt(i);
                if (c >= 'A' && c <= 'Z') {
                    rule2[0] = 1;
                } else if (c >= 'a' && c <= 'z') {
                    rule2[1] = 1;
                } else if (c >= '0' && c <= '9') {
                    rule2[2] = 1;
                } else {
                    rule2[3] = 1;
                }
            }

            if (repeated) {
                System.out.println("NG");
                continue;
            }

            int value = 0;
            for (byte b : rule2) {
                value += b;
            }
            if (value < 3) {
                System.out.println("NG");
                continue;
            }


            System.out.println("OK");
        }
    }

    private static boolean isChildStr(String s, int l, int r) {
        if(s.length() - r <2) {
            return false;
        }

        String lstr = s.substring(l, l + 3);
        String rstr = s.substring(r);
        if (rstr.contains(lstr)) {
//            System.out.println(l + ":" + lstr);
            return true;
        }
        return false;
    }

}

全部评论

相关推荐

joecii:如果没有工资,那可能没有工资是这家公司最小的问题了
找实习记录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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