0字符串中等 HJ20 密码验证合格程序

HJ20 密码验证合格程序

描述

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有长度大于2的不含公共元素的子串重复 (注:其他符号不含空格或换行)

数据范围:输入的字符串长度满足 1 \le n \le 100 \1≤n≤100

分析

难在判断字符串有没有重复的子串
可以双指针遍历判断有没有重复,也可以直接调用String查询子串的方法。

import java.util.*;
public class Main{
    public static void main(String[] args){
        List<String> li = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            li.add(sc.nextLine());
        }
        process(li);
    }
    
    public static void process(List<String> li){
        int up = 0, low = 0, digit = 0, other = 0;
        for(String str : li){
            int len = str.length();
            if(len <= 8){
                System.out.println("NG");
                continue;
            }
            int a=0, b=0, c=0, d=0;
        for(int i=0; i<str.length(); i++){
            char ch = str.charAt(i);
            if(ch>='A' && ch<='Z') a=1;
            else if(ch>='a' && ch<='z') b=1;
            else if(ch>='0' && ch<='9') c=1;
            else d=1;
        }
            if(a+b+c+d<3) {
                System.out.println("NG");
                continue;
            }
            if(isRepetitive(str)){
                System.out.println("NG");
                continue;
            }
            System.out.println("OK");
        }
    }
    
    public static boolean isRepetitive(String s){
        int n = s.length();
        for(int i = 0; i < n-2; i++){
            int tmp = i;
            for(int j = i + 3; j < n; j++){
                if(s.charAt(i) != s.charAt(j)){
                    continue;
                }
                int count = 0;
                while(j < n && s.charAt(i) == s.charAt(j)){
                    i++;
                    j++;
                    count++;
                }
                if(count > 2){
                    return true;
                }
            }
            i = tmp;
        }
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值