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

密码验证合格程序

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

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String line = in.nextLine();
            if (vetify(line)) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }

    public static boolean vetify(String pwd) {
        if (pwd.length() <= 8) {
            return false;
        }

        int alpha = 0, ALPHA = 0, num = 0, symbol = 0;
        for (int i = 0; i < pwd.length(); i++) {
            char c = pwd.charAt(i);
            if (c >= 'a' && c <= 'z') {
                alpha = 1;
            } else if (c >= 'A' && c <= 'Z') {
                ALPHA = 1;
            } else if (c >= '0' && c <= '9') {
                num = 1;
            } else if (c != ' ' || c != '\n') {
                symbol = 1;
            }
        }
        int sum = alpha + ALPHA + num + symbol;

        if (sum >= 3 && checkSubString(pwd)) {
            return true;
        }

        return false;
    }

    public static boolean checkSubString(String pwd) {
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < pwd.length() - 3; i++) {
            String sub = pwd.substring(i, i + 3);
            if (map.containsKey(sub)) {
                return false;
            } else {
                map.put(sub, map.getOrDefault(sub, 0) + 1);
            }
        }

        return true;
    }
}

全部评论

相关推荐

优秀的大熊猫在okr...:多益:此贼,必有同谋,按律,该当连坐!
你不能接受的企业文化有哪...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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