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

密码验证合格程序

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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while(reader.ready()) {
            String input = reader.readLine();
            if(input.length() <= 8) { 
                System.out.println("NG");
                continue;
            }
            
            boolean exist_upper = false;
            boolean exist_lower = false;
            boolean exist_number = false;
            boolean exist_other = false;
            for(int i = 0;i < input.length();i++) {
                char temp = input.charAt(i);
                if(temp-'A'>=0 && temp-'A'<26) {
                    exist_upper = true;
                }else if(temp-'a'>=0 && temp-'a'<26) {
                    exist_lower = true;
                }else if(temp-'0'>=0 && temp-'0'<10) {
                    exist_number = true;
                }else exist_other = true;
            }
            int code_type = 0;
            if(exist_upper) code_type++;
            if(exist_lower) code_type++;
            if(exist_number) code_type++;
            if(exist_other) code_type++;
            if(code_type < 3) {
                System.out.println("NG");
                continue;
            }
            
            boolean isContains = false;
            for(int i = 0;i < input.length()-5;i++) {
                String temp = input;
                String zi = temp.substring(i, i+3);
                String zhu = temp.substring(i+3);
                if(contains(zhu, zi)) {
                    System.out.println("NG");
                    isContains = true;
                    break;
                }
            }
            
            if(isContains) continue;
            else System.out.println("OK");
        }
        
        reader.close();
    }
    private static boolean contains(String zhu,String zi) {
        for(int i = 0;i < zhu.length()-2;i++) {
            String temp = zhu.substring(i, i+3);
            if(temp.equals(zi)) return true;
        }
        return false;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务