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

密码验证合格程序

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

不用正则表达式的做法,纯靠算法,通俗易懂

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()){
        String pwd = sc.nextLine();
        if(pwd.length() <= 8){
            System.out.println("NG");
            continue;
        }
            int countKind = 0;
        String newPwd = pwd.trim();
        if(hasNum(newPwd)){
            countKind++;
        }
        if(hasSmall(newPwd)){
            countKind++;
        }
        if (hasBig(newPwd)){
            countKind++;
        }
        if(hasOther(newPwd)){
            countKind++;
        }
        if(countKind < 3){
            System.out.println("NG");
            continue;
        }
        if(contain(newPwd)){
            System.out.println("NG");
            continue;
        }
        System.out.println("OK");
    }
}

public static boolean contain(String newPwd){
    char[] chars = newPwd.toCharArray();
    for (int i = 0; i < chars.length-2;i++){
        String sub = newPwd.substring(i,i+3);
         String subPwd = newPwd.substring(i+2);
        if(containDupl(sub)){
            continue;
        }
        if(containSubString(subPwd,sub)){
            return true;
        }
    }
    return false;
}

public static boolean hasBig(String pwd){
    char[] chars = pwd.toCharArray();
    for (char c : chars) {
        if(c>='A' && c <='Z'){
            return true;
        }
    }
    return false;
}

public static boolean hasSmall(String pwd){
    char[] chars = pwd.toCharArray();
    for (char c : chars) {
        if(c>='a' && c<='z'){
            return true;
        }
    }
    return false;
}

public static boolean hasNum(String pwd){
    char[] chars = pwd.toCharArray();
    for (char c : chars) {
        if(c>='0' && c <='9'){
            return true;
        }
    }
    return false;
}

public static boolean hasOther(String pwd){
       char[] chars = pwd.toCharArray();
    for (char c : chars) {
        if(!hasBig(c+"") && !hasSmall(c+"") &&!hasNum(c+"")){
            return true;
        }
    }
    return false;
}

public static boolean containSubString(String pwd,String sub){
    if(pwd.contains(sub)){
        return true;
    }
    return false;
}

public static boolean containDupl(String str){
    char[] chars = str.toCharArray();
    for (int i = 0; i < chars.length-1; i++) {
        if(chars[i]==chars[i+1]){
            return true;
        }
    }
    return false;
}

}

全部评论

相关推荐

双非坐过牢:非佬,可以啊10.28笔试,11.06评估11.11,11.12两面,11.19oc➕offer
点赞 评论 收藏
分享
评论
点赞
2
分享
牛客网
牛客企业服务