Huawei Online Programming Questions Series-20-Password Verification Program

Problem Description

1. The question involves knowledge points.

  • String handling.

2. Solve it yourself.

  • Judging the three required conditions in three steps, if one does not meet the direct return.
  • The password is complicated, and it uses the interval to judge the ascii value. In a certain interval, record 1 for the flag bit, and then determine the number of the mark.
  • Substring judgment is made by string.contains(key)making judgments.
package com.chaoxiong.niuke.huawei;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Create by tianchaoxiong on 18-5-6.
 */
public class HuaWei_20 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Boolean>arrayList = new ArrayList<Boolean>();
        while (scanner.hasNext()) {
            String key = scanner.next();
            boolean isOk = isOK(key);
            arrayList.add(isOk);
        }
        for (boolean each:arrayList){
            if(each){
                System.out.println("OK");
            }else {
                System.out.println("NG");
            }
        }
    }
    private static boolean isOK(String key) {
        int len = key.length();
        //1:判断长度
        boolean ng1 = (len <= 8);
        if(ng1)
            return false;
        //2:判断密码复杂度
        boolean ng2 = (getPWLevel(key)<3);
        if(ng2)
            return false;
        //3:判断是否含有超过两个长度的子串
        boolean ng3 =hasSameSubStr(key);
        if(ng3)
            return false;
//        System.out.println(ng1);
//        System.out.println(ng2);
//        System.out.println(ng3);
        return true;
    }

    private static boolean hasSameSubStr(String key) {
        char[]charArr = key.toCharArray();
        for(int i=0;i<key.length()-3;i++){
            String str1 = new String(charArr,i,3);
            String str2 = key.substring(i+2);
            if(str2.contains(str1))
                return true;
        }
        return false;
    }

    private static int getPWLevel(String key) {
        int []intArr = new int[4];

        for(char c:key.toCharArray()){
            //数字
            if(48 <= (int)c&&(int)c <= 57) {
                intArr[0] = 1;
            }else {
                //大写
                if(65 <= (int)c&&(int)c <= 90) {
                    intArr[1] = 1;
                }else {
                    //小写
                    if(97 <= (int)c&&(int)c <= 122) {
                        intArr[2] = 1;
                    }else {
                        intArr[3] = 1;
                    }
                }
            }
        }
        int count = 0;
        for(int each :intArr){
            if(each==1)
                count++;
        }
        return count;
    }
}

3. Quality answers.

similar

4. Summary of this question.

Another difficulty of this question is the input problem . The question does not say how many times it needs to be input. Therefore, an infinite loop needs to be done in the program.

while (scanner.hasNext()) {
    //函数体. 把处理结果收集起来.
}

Print the collected results outside the recycle body.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860377&siteId=291194637