One Solution


  • 0
    A
    int countChange(string s)
    {
        int countL = 0, countU = 0, countD = 0;
        for (int i = 0; i < s.size(); i ++)
        {
            if(islower(s[i]))
            {
                countL = 1;
            }else if(isupper(s[i]))
            {
                countU = 1;
            }else if(isdigit(s[i]))
            {
                countD = 1;
            }
        }
        return countD + countL + countU;
    }
    int strongPasswordChecker(string s) {
       // cout << s.size() << endl;
        int res;
        int leastChange = 0;
        vector< vector<int> > repeat(3);
        int cur = 1;
        for (int i = 1; i < s.size(); i ++)
        {
            if(s[i] == s[i - 1])
            {
                cur ++;
            }else{
                if(cur >= 3)
                {
                    repeat[cur % 3].push_back(cur);
                }
                cur = 1;
            }
            if(cur % 3 == 0)
            {
                leastChange ++;
            }
        }
        if(cur >= 3)
        {
            repeat[cur % 3].push_back(cur);
        }
        int trivialChange = 3 - countChange(s);
        if(s.size() < 6)
        {
            int leastadd = 6 - s.size();
            if(leastadd >= trivialChange)
            {
                trivialChange = 0;
            }else{
                leastadd = trivialChange;
            }
            int nums = leastChange * 3 + repeat[1].size() + repeat[2].size() * 2;
            if(nums == 5)
            {
                return max(2, leastadd);
            }else{
                return leastadd;
            }
        }else if(s.size() <= 20)
        {
            if(leastChange >= trivialChange)
            {
                trivialChange = 0;
            }else{
                leastChange = trivialChange;
            }
            return leastChange;
        }else{
            int leastErase = s.size() - 20;
            int use = 1;
            while(use != 0 && leastErase != 0)
            {
                for (int i = 0; i < 3; i ++)
                {
                    if(leastErase < i + 1)
                        break;
                    if(repeat[i].size() != 0)
                    {
                        int len = repeat[i].back();
                        repeat[i].pop_back();
                        use = i + 1;
                        len -= use;
                        leastChange --;
                        if(len >= 3)
                        {
                            repeat[len % 3].push_back(len);
                        }
                        break;
                    }
                }
                leastErase -= use;
            }
            if(leastChange > trivialChange)
            {
                trivialChange = 0;
            }else{
                leastChange = trivialChange;
            }
            int res = s.size() - 20 + leastChange;
            return res;
        }
    }
    
    

Log in to reply
 

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