Strong Password Checker

A password is considered strong if below conditions are all met:

  1. It has at least 6 characters and at most 20 characters.
  2. It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit.
  3. It must NOT contain three repeating characters in a row ("...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met).

Write a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM change required to make s a strong password. If s is already strong, return 0.

Insertion, deletion or replace of any one character are all considered as one change.

思路:垃圾題,一點算法都沒有。抄的答案;我看不出這裏面有什麼好考察的,我覺得出這個題的人水平也不怎麼高。

class Solution {
   public int strongPasswordChecker(String s) {
        int one = 0, two = 0, chg = 0, p = 0, l = s.length(), r = 0, up = 0, lo = 0, d = 0;
        while (p < l) {
            char c = s.charAt(p);
            if (Character.isUpperCase(c)) up = 1;
            if (Character.isLowerCase(c)) lo = 1;
            if (Character.isDigit(c)) d = 1;
            if (p > 1 && c == s.charAt(p - 1) && c == s.charAt(p - 2)) {
                r = 2;
                while (p < l && s.charAt(p) == c) {
                    p++;
                    r++;
                }
                if (r % 3 == 0) one++;
                else if(r % 3 == 1) two++;
                chg += r / 3;
            } else p++;
        }
        int miss = 3 - up - lo - d;
        if (l < 6) {
            return Math.max(miss, 6 - l);
        } else if (l <= 20) {
            return Math.max(miss, chg);
        } else{
            int del = l - 20;
            chg -= Math.min(del, one);
            chg -= Math.min(Math.max(del - one, 0), two * 2) / 2;
            chg -= Math.max(del - one - 2 * two, 0) / 3;
            return del + Math.max(chg, miss);
        }
    }
}

 

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