【牛客】密碼驗證程序

鏈接:https://www.nowcoder.com/questionTerminal/184edec193864f0985ad2684fbc86841?orderByHotValue=1&questionTypes=000100&mutiTagIds=579&page=1&onlyReference=false
來源:牛客網

密碼要求:

1.長度超過8位

2.包括大小寫字母.數字.其它符號,以上四種至少三種

3.不能有相同長度超2的子串重複

說明:長度超過2的子串

輸入描述:

一組或多組長度超過2的子符串。每組佔一行

輸出描述:

如果符合要求輸出:OK,否則輸出NG

解題思路:主要是重複字符串子串那裏需要你進行切割判斷,這裏需要利用String中的contains函數判斷從這個子串之後是否有重複的,注意:長度大於2的子串。

import java.util.*;

public class Main {
    public static int panduan(String s){
        int sum = 0;
        int a = 0,b = 0,c = 0,d = 0;
        char[] chars = s.toCharArray();
        for(int i = 0;i<chars.length;i++){
            if(chars[i] >= 'A' && chars[i] <='Z'){
                a = 1;
            }else if(chars[i] >= 'a' && chars[i] <='z'){
                b = 1;
            }else if(chars[i] >= '0' && chars[i] <='9'){
                c = 1;
            }else {
                d = 1;
            }
        }
        sum = a+b+c+d;
        return sum;
    }
    public static boolean panduan2(String s){
        for(int i = 0;i<s.length()-2;i++){
            String substring = s.substring(i, i + 3);
            if(s.substring(i+3).contains(substring))
                return false;
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String s = sc.nextLine();
            if(s.length()<=8){
                System.out.println("NG");
            }else {
                if(panduan(s)<3){
                    System.out.println("NG");
                }else {
                    if (panduan2(s)){
                        System.out.println("OK");
                    }else {
                        System.out.println("NG");
                    }
                }
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章