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

密码验证合格程序

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;
    }
}

全部评论

相关推荐

07-23 15:05
门头沟学院 Java
熊大不大:不好意思KPI数据刚刚刷新,刚刚达标
点赞 评论 收藏
分享
07-23 12:04
门头沟学院 Java
现在是很缺人吗
码农索隆:缺分母,不缺分子,这样好作为炫耀的资本
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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