My C++ AC Code


  • 0
    J

    '''

    static bool cmp(int a, int b)
    {
        return a % 3 > b % 3;
    }
    
    int strongPasswordChecker(string s) {
        deque<int> cnts;
        int t = 0, lowercase = 1, uppercase = 1, digit = 1;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] >= 'a' && s[i] <= 'z') lowercase = 0;
            else if (s[i] >= 'A' && s[i] <= 'Z') uppercase = 0;
            else if (s[i] >= '0' && s[i] <= '9') digit = 0;
            
            if (i == 0)
            {
                t = 1;
            }else
            {
                if (s[i] == s[i - 1])
                {
                    t++;
                }
                else
                {
                    if (t > 2) cnts.push_back(t);
                    t = 1;
                }
            }
        }
        if (t > 2) cnts.push_back(t);
        
        sort(cnts.begin(), cnts.end(), cmp);
    
        int n = s.size();
        int count = 0;
        if (n > 20)
        {
            count = n - 20;
            int t = n - 20;
            while (t && !cnts.empty())
            {
                int x = cnts.front() % 3;
                if (x == 0)
                {
                    x = cnts.front();
                    if (x == 0)
                    {
                        cnts.pop_front();
                        continue;
                    }
                }
                
                if (t >= x)
                {
                    t -= x;
                    x = cnts.front() - x;
                    cnts.pop_front();
                    cnts.push_back(x);
                }
                else
                {
                    x  = cnts.front() - t;
                    t = 0;
                    cnts.pop_front();
                    cnts.push_back(x);
                }
            }
        }
    
        int tc = 0;
        for (int x : cnts) tc += x / 3;
        int cc = lowercase + uppercase + digit;
        
        if (n < 6)
        {
            count = max(6 - n, cc);
            count = max(count, tc);
            cc = 0;
            tc = 0;
        }
        
        count = count + max(tc, cc);
        return count;
    }
    

    '''


Log in to reply
 

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