题解 | 密码验证合格程序

密码验证合格程序

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

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.hasNextLine()) { // 注意 while 处理多个 case
            String pwd = in.nextLine();
            if (pwd.length() <= 8) {
                System.out.println("NG");
                continue;
            } else {
                int digit = 0;
                int bigLetter = 0;
                int smallLetter = 0;
                int other = 0;
                for (int i = 0; i < pwd.length(); i++) {
                    char c = pwd.charAt(i);
                    if (Character.isDigit(c)) {
                        digit++;
                    } else if (Character.isUpperCase(c)) {
                        bigLetter++;
                    } else if (Character.isLowerCase(c)) {
                        smallLetter++;
                    } else {
                        other++;
                    }
                }

                int zeroCnt = 0;
                if (digit == 0) {
                    zeroCnt++;
                }
                if (bigLetter == 0) {
                    zeroCnt++;
                }
                if (smallLetter == 0) {
                    zeroCnt++;
                }
                if (other == 0) {
                    zeroCnt++;
                }
                // 有2种以上字符类型为0,打印NG
                if (zeroCnt >= 2) {
                    System.out.println("NG");
                    continue;
                }

            
                boolean ng = false;
                out:
                for (int i = 0; i < pwd.length(); i++) {
                    
                    int repeatLength = 3;
                    for (int j = 0 + i; j < pwd.length() / 2; j++) {
                        for (int k = 0 + i; k < pwd.length() / 2; k++) {
                            if (k + repeatLength >= pwd.length()) {
                                break;
                            }
                            String str = pwd.substring(k, k + repeatLength);
                            //System.out.print(str  + "->" + pwd.replace(str, ""));
                            // 替换掉截取的字符串后,如果替换后的字符串长度不止少了repeatLength,说明替换了多次,并且说明出现了重复的字符串
                            if (pwd.replace(str, "").length() < pwd.length() - repeatLength) {
                                System.out.println("NG");
                                ng = true;
                                break out;
                            }
                            //System.out.println();
                        }
                        repeatLength++;
                    }
                }
                if (!ng) {
                    System.out.println("OK");
                }
            }
        }
    }
}

全部评论

相关推荐

AC鸽想进大厂:你是我见过最美的牛客女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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