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

密码验证合格程序

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

细测的话应该还有bug,但提交时系统的测试用例是可以全部通过的。

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            if (s.length() < 8) {
                System.out.println("NG");
            } else {
                String s1 = s.replaceAll("[A-Z]", "");
                String s2 = s.replaceAll("[a-z]", "");
                String s3 = s.replaceAll("[0-9]", "");
                String s4 = s.replaceAll("[A-Za-z0-9 ]", "");

                int An = s.length() - s1.length();//大写字母的数量
                int an = s.length() - s2.length();//小写字母的数量
                int n0 = s.length() - s3.length();//数字的数量
                int othern = s4.length();//其他字符的数量

                int[] nums = {An, an, n0, othern};
                int numo = 0;
                for (int i = 0; i < 4; i++) {
                    if (nums[i] == 0) numo += 1;//等于0代表密码中不含该种类型的字符
                }
                
                if (numo > 1) {
                    System.out.println("NG");
                } else { 
                    String out ="OK";
                    //判断是否有长度大于2的重复子串              
                    for (int k = 0; k < s.length() / 3; k++) {
                        String ssk = s.substring(k, k + 3);
                        String sso = s.substring(k + 3);
                        if(sso.contains(ssk)){
                            out="NG";
                            break;
                        }

                    }
                    System.out.println(out);


                }


            }
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务