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

密码验证合格程序

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

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.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            boolean checkResult = checkLength(str) && checkTypeNum(str) && checkRepeat(str);
            System.out.println(checkResult ? "OK" : "NG");
           
        }
    }
    
    private static boolean checkLength(String str){
        return str.length() > 8;
    }
    
    private static boolean checkTypeNum(String str){
        int res = 0;
        for(int i=0; i<str.length(); i++){
            char c = str.charAt(i);
//             System.out.println(c==65);
            if(c >= 48 && c <= 57){
                res |= 1;
            }else if(c >= 65 && c <= 90){
                res |= 1<<1;
            }else if(c >= 97 && c <= 122){
                res |= 1<<2;
            }else if(c != 32){
                res |= 1<<3;
            }
        }
        return Arrays.asList(7,11,13,14,15).contains(res);
    }
    
    private static boolean checkRepeat(String str){
        List<String> subList = new ArrayList();
        for(int i=0; i<str.length()-2; i++){
            subList.add(str.substring(i, i+3));
        }
        Set<String> subSet = new HashSet<>(subList);
//         System.out.println(subList);
//         System.out.println(subSet);
        return subList.size() == subSet.size();
    }
}

全部评论

相关推荐

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