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

密码验证合格程序

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.hasNextLine()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            if (s.length() > 8 && checkSymbol(s) && isRepeat(s)) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }
    static boolean isRepeat(String s) {
        for (int i = 0; i < s.length() - 2; i++) {
            String sub = s.substring(i, i + 3);
            if (s.indexOf(sub) != s.lastIndexOf(sub)) {//从前后查找第一次出现子串位置,如果不相等说明重复
                return false;
            }
        }
        return true;
    }

    static boolean checkSymbol(String s) {
        int a = 0, b = 0, c = 0, d = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (Character.isLowerCase(ch)) {//Character工具类判别字符类型好用
                a = 1;
            } else if (Character.isUpperCase(ch)) {
                b = 1;
            } else if (Character.isDigit(ch)) {
                c = 1;
            } else {
                d = 1;
            }
            if (a + b + c + d >= 3) {
                return true;
            }
        }
        return false;
    }
}

全部评论

相关推荐

给个offer灞:校友 是不是金die
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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