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

密码验证合格程序

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

因为有点不太理解第三个条件... 看了下题解这位大神说的就理解了: alt

import java.util.*;
import java.util.regex.Pattern;

/*
* 密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)
* */
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();
            if (length(str) && match(str) && isRepeat(str, 0, 3)) {
                System.out.println("OK");
            } else
                System.out.println("NG");
        }
    }

        //1.长度超过8位
    private static boolean length(String str){
        if(str.length() <= 8){
            return false;
        }else
            return true;
    }

//      2.包括大小写字母.数字.其它符号,以上四种至少三种
    private static boolean match(String str) {
        int num = 0;
        Pattern compile1 = Pattern.compile("[A-Z]");
        if (compile1.matcher(str).find()) {
            num++;
        }
        Pattern compile2 = Pattern.compile("[a-z]");
        if (compile2.matcher(str).find()) {
            num++;
        }
        Pattern compile3 = Pattern.compile("[0-9]");
        if (compile3.matcher(str).find()) {
            num++;
        }
        Pattern compile4 = Pattern.compile("[^A-Za-z0-9]");
        if (compile4.matcher(str).find()) {
            num++;
        }
        if (num >= 3) {
            return true;
        } else
            return false;
    }

    //      3.不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)
    private static boolean isRepeat(String str,int sta,int end) {
        if(end >= str.length()){
            return true;
        }
        if (str.substring(end).contains(str.substring(sta, end))) {
            return false;
        }else
            return isRepeat(str,sta + 1,end + 1);
    }
}
全部评论

相关推荐

06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
点赞 评论 收藏
分享
06-27 18:53
门头沟学院 Java
这样才知道自己不适合搞代码,考公去咯
只爱喝白开水:我也发现不适合搞代码,打算转非技术方向了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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