Please add test case: "aaaaAAAAAA000000123456"


  • 0
    H

    We should add a test case: "aaaaAAAAAA000000123456", answer is 5.

    After playing with all my wrong answer test cases (yep, I have nothing better to do..), I designed the following solution which will pass current test cases but produce 6 for the above answer:

    public int strongPasswordChecker(String s) {
            int add = 0, delete = 0, exceed = Math.max(0, s.length()-20), replace = 0, missing = 0;
            boolean upper = false, lower = false, digit = false;
            for (char c : s.toCharArray()) {
                if (c >= 'A' && c <= 'Z') upper = true;
                if (c >= 'a' && c <= 'z') lower = true;
                if (c >= '0' && c <= '9') digit = true;
            }
            if (!upper) missing++;
            if (!lower) missing++;
            if (!digit) missing++;
            List<Integer> lenlist = new ArrayList<>();
            int len = 1;
            for (int i = 1; i <= s.length(); i++) {
                if (i == s.length() || s.charAt(i) != s.charAt(i-1)) {
                    if (len >= 3) lenlist.add(len);
                    len = 1;
                } else {
                    len++;
                }
            }
            Collections.sort(lenlist);
            for (int rep : lenlist) {
                // deal with repeating chars of length=rep
                //System.out.println("rep="+rep);
                if (s.length() + add < 6) {//add
                    add++;
                    if (missing > 0) missing--;
                } else if (s.length() + add - delete <= 20) {//replace
                    replace += rep/3;
                    missing = Math.max(0, missing-rep/3);
                } else {//add = 0, s.length()-delete > 20
                    while (rep >= 3 && rep-2 > exceed) {
                        rep -= 3;
                        replace++;
                        if (missing > 0) missing--;
                    }
                    if (rep >= 3) {
                        exceed -= rep-2;
                        delete += rep-2;
                    }
                }
            }
            //System.out.println(add+","+delete+","+replace+","+missing);
            if (s.length()+add < 6) {
                int d = 6 - s.length() - add;
                missing = Math.max(0, missing-d);
                return add + d + missing;
            }
            
            return add + replace + missing + delete + exceed;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.