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

密码验证合格程序

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

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;

public class Main {
    public static void main(String[] args) throws IOException {

        testTh();
    }

    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;

        while ((str = bf.readLine()) != null) {
            if (str.length() < 8) {
                System.out.println("NG");
                continue;
            }
            char[] chars = str.toCharArray();
            int flag_number = 0;
            int flag_a = 0;
            int flag_A = 0;
            int flag_o = 0;
            for (char aChar : chars) {
                if (aChar >= '0' && aChar <= '9') {
                    flag_number = 1;
                    continue;
                }
                if ((aChar >= 'a' && aChar <= 'z')) {
                    flag_a = 1;
                    continue;
                }
                if ((aChar >= 'A' && aChar <= 'Z')) {
                    flag_A = 1;
                    continue;
                }
                if (aChar != ' ' && aChar != '\n') {
                    flag_o = 1;
                }
            }
            if (flag_a + flag_A + flag_o + flag_number < 3) {
                System.out.println("NG");
                continue;
            }
            int max = 0;
            for (int i = 0; i < chars.length; i++) {
                char aChar = chars[i];
                int j = i;
                ArrayList<Integer> al = new ArrayList<>();
                for ( j = i + 3; j < chars.length; j++) {
                    if (aChar == chars[j]) {
                        al.add(j);
                    }
                }
                if (al.size() != 0) {
                    int temp = 0;
                    while (temp != al.size()) {
                        int y = al.get(temp);
                        temp++;
                        int count = 0;
                        int x = i;
                        while (chars[x] == chars[y]) {
                            count++;
                            x++;

                            y++;
                            if (y >= chars.length - 1) {
                                break;
                            }
                        }
                        if (count > max) {
                            max = count;
                        }
                    }
                }

            }
            if (max > 2) {
                System.out.println("NG");
            } else {
                System.out.println("OK");
            }
        }


    }




}

全部评论

相关推荐

11-11 14:21
西京学院 C++
无敌混子大王:首先一点,不管学校层次怎么样,教育经历放在第一页靠上位置,第一页看不到教育经历,hr基本直接扔掉了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务