Java easy understand solution


  • 0
    R
    public class Solution {
        // different qu used to record those repeated character's length
        // 0, 1, 2 mean their length remainder % 3
        Queue<Integer> qu0 = new LinkedList<>();
        Queue<Integer> qu1 = new LinkedList<>();
        Queue<Integer> qu2 = new LinkedList<>();
        int missing = 0;
        int res = 0;
    
        public int strongPasswordChecker(String s) {
            if(s == null || s.length() <= 2) return 6-s.length();
            int count = 0;
            int l = s.length();
            boolean lower = false, upper = false, digit = false;
            for (int i = 0; i < l; i++) {
                char c = s.charAt(i);
                lower = lower || Character.isLowerCase(c);
                upper = upper || Character.isUpperCase(c);
                digit = digit || Character.isDigit(c);
                if (i > 0 && s.charAt(i) == s.charAt(i - 1)) count++;
                else {
                    if (count >= 3) offerq(count);
                    count = 1;
                }
            }
            if (count >= 3) offerq(count);
            if (!lower) missing++;
            if (!upper) missing++;
            if (!digit) missing++;
            
            // need to insert
            if(l < 6) {
                if(!qu0.isEmpty() || !qu1.isEmpty()) {
                    l++;
                    res++;
                    res += Math.max(6-l, 0);
                    missing--;
                    missing -= Math.max(6-l, 0);
                } else if(!qu2.isEmpty()) {
                    res += 2;
                    missing -= 2;
                } else {
                    res += Math.max(6-l, 0);
                    missing -= Math.max(6-l, 0);
                } 
            // need to replace    
            } else if(l <= 20) {
                replace(qu0);
                replace(qu1);
                replace(qu2);
            // need to delete
            } else {
                // firstly delete those %3 == 0 repeated char
                l = delete(qu0, l, 1, 0);
                // secondly delete those %3 == 1 repeated char
                l = delete(qu1, l, 2, 0);
                // thirdly delete those %3 == 2 repeated char
                l = delete(qu2, l, 3, 1);
                // when the length meet 21, we just need to delete 1 char
                if(l == 21 && !qu2.isEmpty()) {
                    int q = qu2.poll();
                    l -= 1;
                    res += q/3 + 1;
                    missing--;
                }
                replace(qu0);
                replace(qu1);
                replace(qu2);
                res += Math.max(l-20, 0);
            }
        
            if(missing > 0) res += missing;
            return res;
        }
        
        private void offerq(int key) {
            if(key < 3) return;
            if(key % 3 == 0) qu0.offer(key);
            else if(key % 3 == 1) qu1.offer(key);
            else qu2.offer(key);
        }
        
        private void replace(Queue<Integer> qu) {
            while(!qu.isEmpty()) {
                int q = qu.poll();
                res += q/3;
                missing -= q/3;
            }
        }
        
        private int delete(Queue<Integer> qu, int l, int i, int offset) {
            while(l > (20+offset) && !qu.isEmpty()) {
                int q = qu.poll();
                l -= i;
                res += i;
                if(q > (i+2))
                    qu2.offer(q-i);
            } 
            return l;
        }
    }
    

Log in to reply
 

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