华为机试_HJ20_密码验证合格程序)

描述

密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
示例1
输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出:
OK
NG
NG
OK

小白猿的自我救赎:

运行时间:35ms 占用内存:10644KB

import java.util.Scanner;
import java.util.BitSet;

public class Main{
    public static void main(String[] arg){
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String password = in.nextLine();
            BitSet bitSet = new BitSet(4);
 
            if (password.length() > 8) {
                boolean err = true;
                for (int i=0;i<password.length();i++) {
                	char s = password.charAt(i);
                    if ( s>= '0' && s <= '9') {
                    	bitSet.set(0);
                    } else if (s >= 'a' && s <= 'z') {
                    	bitSet.set(1);
                    } else if (s >= 'A' && s <= 'Z') {
                    	bitSet.set(2);
                    } else if (s == ' ' || s == '\n') {
                        err = false;
                        break;
                    } else {
                    	bitSet.set(3);
                    }
                }
                if (err) {
                    if (bitSet.cardinality() >= 3) {
                        if (repeat(password)) {
                            System.out.println("OK");
                        } else {
                            System.out.println("NG");
                        }
                    } else {
                        System.out.println("NG");
                    }
                } else {
                    System.out.println("NG");
                }
            } else {
                System.out.println("NG");
            }
        }
    }
 
	private static boolean repeat(String s) {

		for(int i =0;i<s.length();i++) {
			for(int j=i+3;j<s.length()-2;j++) {
				String a = s.substring(i,i+3);
				if(a.equals(s.substring(j,j+3))) {
					return false;
				}	
			}
		}
		return true;
	}
}

代码改良:

  1. 有神人相助,助我优化了一下字符串重复那部分的代码。
private static boolean repeat(String s) {
    for(int i=0;i<s.length()-2;i++) {
		if(s.indexOf(s.substring(i,i+3),i+3) != -1) {
			return false;
		}
	}
	return true;
  • 神人说每一次substring 会产生一个新的字符串,所以浪费内存。而 indexof 只是在原来的字符串上查找,所以更好一些。
  1. 原来是用int表示的种类字符出现次数,神人说,用int 做标识索引浪费内存, 可以用更小的数据类型 。于是选择了刚学会的bitSet。

高赞回答借鉴:

运行时间:32ms 占用内存:10804KB

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

public class Main{
    public static void main(String[] arg){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.next();
            if(str.length() <= 8){
                System.out.println("NG");
                continue;
            }
            if(getMatch(str)){
                System.out.println("NG");
                continue;
            }
            if(getString(str, 0, 3)){
                System.out.println("NG");
                continue;
            }
            System.out.println("OK");
        }
    }
    // 校验是否有重复子串
    private static boolean getString(String str, int l, int r) {
        if (r >= str.length()) {
            return false;
        }
        if (str.substring(r).contains(str.substring(l, r))) {
            return true;
        } else {
            return getString(str,l+1,r+1);
        }
    }
    // 检查是否满足正则
    private static boolean getMatch(String str){
        int count = 0;
        Pattern p1 = Pattern.compile("[A-Z]");
        if(p1.matcher(str).find()){
            count++;
        }
        Pattern p2 = Pattern.compile("[a-z]");
        if(p2.matcher(str).find()){
            count++;
        }
        Pattern p3 = Pattern.compile("[0-9]");
        if(p3.matcher(str).find()){
            count++;
        }
        Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");
        if(p4.matcher(str).find()){
            count++;
        }
        if(count >= 3){
            return false;
        }else{
            return true;
        }
    }
}

老实说,大神的代码,我有点看不懂,但是确实比我的节省了不少空间,为了不占空间,我特地用了bitSet也无济于事呢。

知识点:

  • indexOf(String str,int index)
    • 返回从index位置开始查找指定字符str在字符串中第一次出现处的起始索引,如果字符串中没有这样的字符,则返回-1.
  • Pattern类
    图片来自https://blog.csdn.net/qq_35995514/article/details/118273738
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值