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

密码验证合格程序

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String line = sc.nextLine();
            int length = line.length();
            int lowerCase = 0;
            int upperCase = 0;
            int digit = 0;
            int others = 0;
            for (int i = 0; i < length; i++) {
                if (Character.isLowerCase(line.charAt(i))) {
                    lowerCase = 1;
                } else if (Character.isUpperCase(line.charAt(i))) {
                    upperCase = 1;
                } else if (Character.isDigit(line.charAt(i))) {
                    digit = 1;
                } else {
                    others = 1;
                }
            }
            if (length <= 8) {
                System.out.println("NG");
            } else if (lowerCase + upperCase + digit + others < 3) {
                System.out.println("NG");
            } else {
                boolean res = repeat(line);
                if (res) {
                    System.out.println("OK");
                } else {
                    System.out.println("NG");
                }
            }
        }

    }
    public static boolean repeat(String s) {
        int k = s.length() / 2;
        for (int i = 3; i <= k; i++) {
            for (int j = 0; j <= s.length() - i; j++) {
                String substring = s.substring(j, j + i);
                String replace = s.replace(substring, "");
                if (replace.length() < s.length() - substring.length()) {
                    return false;
                }
            }

        }
        return true;
    }
}

全部评论

相关推荐

09-28 22:01
已编辑
广西科技大学 IT技术支持
合适才能收到offe...:找桌面运维?
点赞 评论 收藏
分享
爱读书的放鸽子能手很...:刷个两端实习,冲春招,流水线什么时候不能去
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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