密码验证合格程序

今天写 密码验证合格程序,题目如下:
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复

输入描述:

一组或多组长度超过2的子符串。每组占一行

输出描述:

如果符合要求输出:OK,否则输出NG

示例:

输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出:
OK
NG
NG
OK

源码如下:

import java.util.Scanner;
public class Main{
    public static boolean checkLength(String password){  //判定长度
        if(password.length() > 8){
            return true;
        }
        return false;
    }

    public static boolean password(String password){   //判定密码是否足够三种字符
        String[] str = new String[4];
        int num = 0;
        char[] ch = password.toCharArray();
        for(int i = 0; i < ch.length; i++){
            if(ch[i] >= '0' && ch[i] <= '9'){
                str[0] = "true";
            }else if(ch[i] >= 'a' && ch[i] <= 'z'){
                str[1] = "true";
            }else if(ch[i] >= 'A' && ch[i] <= 'Z'){
                str[2] = "true";
            }else {
                str[3] = "true";

            }
        }
        int count = 0;
        for(int i = 0; i < str.length; i++){
            if(str[i] == "true"){
                count++;
            }
        }
        if(count >=3){
            return true;
        }
        return false;
    }

    public static boolean checkString(String password){  //判定是否含有重复(长度超过2的)子串
        char[] ch = password.toCharArray();
        int left = 0,right = ch.length - 1;
        while(left != right){
            int middle = right - 3;
            if(ch[left] != ch[right]){
                if(left < middle){
                    left++;
                }else{
                    left = 0;
                    right--;
                }
            }
            else{
                int i;
                for(i = 0; i < 2; i++){
                    if(left > 1) {
                        left--;
                        right--;
                    }else {
                        left++;
                    }
                    if(ch[left] != ch[right]){
                        break;
                    }
                }
                if(i == 2){
                    return false;
                }
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            String password = sc.nextLine();
            if(checkLength(password) && checkString(password)
                    && password(password)){
                System.out.println("OK");
            }else{
                System.out.println("NG");
            }

        }
    }
}

但是写完了之后,朋友告诉我了个专门用来判定是否有子串的方法,即substring().contains方法,可以直接找子串,主要是我写的checkString(),实在是太麻烦了,修改后的如下:

public static boolean checkString(String password) {
        for (int i = 0; i < password.length() - 3; i++) {
            if (password.substring(i + 3).contains(password.substring(i,i + 3))){         //从下标 i+1 直到 password.length()-3 寻找是否有 i 到 i+1 的子串;
                return false;
            }
        }
        return true;
    }

这样一看,是不是就简洁明了多了,至少相比起来,方便好用!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运笔如飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值