【好】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
",
        //        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

原文地址:https://www.cnblogs.com/charlesblc/p/5966768.html