好strong-password-checker,我自己做出来的:)

Posted 笨鸟居士的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了好strong-password-checker,我自己做出来的:)相关的知识,希望对你有一定的参考价值。

我自己做出来的,分了几种情况来考虑。(再后面有加了注释的版本)

https://leetcode.com/problems/strong-password-checker/

// 加油!

public class Solution {

    public int strongPasswordChecker(String s) {
        int sLen = s.length();
        if (sLen < 4) {
            return 6 - sLen;
        }
        int lnum = 1;
        int unum = 1;
        int dnum = 1;
        int rcount = 0;
        int ricount = 0;
        int rdcount = 0;
        int sameseq = 0;

        for (int i=0; i<sLen; i++) {
            char ch = s.charAt(i);
            if (ch>=‘a‘ && ch<=‘z‘) {
                lnum = 0;
            }
            if (ch>=‘A‘ && ch<=‘Z‘) {
                unum = 0;
            }
            if (ch>=‘0‘ && ch<=‘9‘) {
                dnum = 0;
            }

            // fix bug
            if (i == 0) {
                sameseq = 1;
            }
            else if (ch != s.charAt(i-1)) {
                if (sameseq >= 3) {
                    // 这个很重要
                    while (sLen + ricount < 6 && sameseq >= 3) {
                        ricount++;
                        sameseq -= 2;
                    }
                    while (sLen - rdcount > 20 && sameseq >= 3) {
                        rdcount++;
                        sameseq --;
                    }
                    rcount += sameseq / 3;
                }
                sameseq = 1;
            }
            else {
                sameseq++;
            }
        }

        // fixbug
        if (sameseq >= 3) {
            // 这个很重要
            while (sLen + ricount < 6 && sameseq >= 3) {
                ricount++;
                sameseq -= 2;
            }
            while (sLen - rdcount > 20 && sameseq >= 3) {
                rdcount++;
                sameseq --;
            }
            rcount += sameseq / 3;
        }

        //System.out.printf("rcount: %d, ricount: %d, rdcount: %d, lnum: %d, unum: %d, dnum: %d\n",
        //        rcount, ricount, rdcount, lnum, unum, dnum);

        int update = lnum + unum + dnum;
        int must = ricount + rcount;
        if (sLen + ricount < 6) {
            must += 6 - sLen - ricount;
        }
        if (sLen < 20) {
            return must > update ? must : update;
        }

        // 跟上面的不一样,因为删除字符是无法增加新的类型的
        if (sLen - rdcount > 20) {
            rdcount += sLen - rdcount - 20;
        }
        return rcount >= update ? rcount + rdcount : update + rdcount;

    }
    
}

 

以下是加了注释的版本:

public class Solution {

    public int strongPasswordChecker(String s) {
        int sLen = s.length();
        if (sLen < 4) {
            return 6 - sLen;
        }

        int lnum = 1; // need lower
        int unum = 1; // need upper
        int dnum = 1; // need digit

        int rcount = 0;  // count need to replace repeated seq
        int ricount = 0; // count need to add in repeated seq
        int rdcount = 0; // count need to remove from repeated seq
        int sameseq = 0; // count of chars in repeated seq

        for (int i=0; i<sLen; i++) {
            char ch = s.charAt(i);
            if (ch>=‘a‘ && ch<=‘z‘) {
                lnum = 0;
            }
            if (ch>=‘A‘ && ch<=‘Z‘) {
                unum = 0;
            }
            if (ch>=‘0‘ && ch<=‘9‘) {
                dnum = 0;
            }

            // check repeated seq
            if (i == 0) {
                sameseq = 1;
            }
            else if (ch != s.charAt(i-1)) {
                if (sameseq >= 3) {
                    // if shorter length, add char into repeated seq
                    while (sLen + ricount < 6 && sameseq >= 3) {
                        ricount++;
                        sameseq -= 2;
                    }
                    // if longer length, remove char from repeated seq
                    while (sLen - rdcount > 20 && sameseq >= 3) {
                        rdcount++;
                        sameseq --;
                    }
                    // if length matches, replace char in repeated seq
                    rcount += sameseq / 3;
                }
                sameseq = 1;
            }
            else {
                sameseq++;
            }
        }

        // need check repeated seq after loop
        if (sameseq >= 3) {
            // as previous process
            while (sLen + ricount < 6 && sameseq >= 3) {
                ricount++;
                sameseq -= 2;
            }
            while (sLen - rdcount > 20 && sameseq >= 3) {
                rdcount++;
                sameseq --;
            }
            rcount += sameseq / 3;
        }

        int update = lnum + unum + dnum;
        int must = ricount + rcount;
        if (sLen + ricount < 6) {
            must += 6 - sLen - ricount;
        }
        if (sLen < 20) {
            return must > update ? must : update;
        }

        // if longer length, use below process
        if (sLen - rdcount > 20) {
            rdcount += sLen - rdcount - 20;
        }
        return rcount >= update ? rcount + rdcount : update + rdcount;

    }
    
}

准备发表在Discuss版:

https://discuss.leetcode.com/category/549/strong-password-checker

以上是关于好strong-password-checker,我自己做出来的:)的主要内容,如果未能解决你的问题,请参考以下文章

有人数据结构好,有人离散数学好

线路板要镀金好还是镀镍好呢???大家给点意见得无,紧急求助..

学习ui设计是自学好?还是参加培训好?

RPC好,还是RESTful好?

怎样才算是好程序员?关于好程序员与好代码的杂谈

jQuery 根据日期的不同时间段,做问候语:中午好,下午好,早上好