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

密码验证合格程序

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

import java.util.*;
public class Main{
    public static Boolean checkPassword(String str){
        // 验证长度
        if(str.length() <= 8) return false;
        // 验证包括的字符种类
        int low = 0, up = 0, num = 0, other = 0;
        for(int i = 0; i < str.length(); i++){
            char tmp = str.charAt(i);
            if(tmp >= 'a' && tmp <= 'z'){
                low = 1;
            }
            else if(tmp >= 'A' && tmp <= 'Z'){
                up = 1;
            }
            else if(tmp >= '0' && tmp <= '9'){
                num = 1;
            }
            else{
                other = 1;
            }
        }
        if(low+up+num+other < 3) return false;
        // 验证是否有长度大于2的子串重复
        //  这个判断子串的方式是,将一句话截成两段,只要后面一段不包含前面那段就可以了
        for(int i = 0; i < str.length(); i++){
            for(int j = i+3; j < str.length(); j++){
                String tmp = str.substring(i, j);
                if(str.substring(j).contains(tmp)){
                    return false;
                }
            }
        }
        // 以上false都没有出现,返回true
        return true;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String str = sc.nextLine();
            if(checkPassword(str)){
                System.out.println("OK");
            }
            else{
                System.out.println("NG");
            }
        }
    }
}
全部评论

相关推荐

昨天 16:13
嘉应学院 Python
xiaolihuam...:很明显骗子,如果是hr直接约你面试了,哪用得着内推,如果是员工的话,你得多优秀,一线员工直接加你微信,
点赞 评论 收藏
分享
07-01 17:14
中北大学 Java
兄弟们是真是假
牛客46374834...:我在boss上投java岗从来没成功过
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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