dumb but easy to understand 6ms C++


  • 0
    T

    could be shorter and more clear
    Using ASCII to compare the sum of the word

    class Solution {
    public:
        bool detectCapitalUse(string word) {
            int sum = 0, size = word.size(), sum2 = 0;
            bool alllow = true, allup = true, firstup = isupper(word[0])?true:false;
            if(!size) return false;
    
            for(auto i : word){
                sum += i;
                if(i!=word[0]) sum2 += i;
                if(isupper(i)) alllow = false;
                if(islower(i)) allup = false;
                if(i!=word[0] && isupper(i)) firstup = false;
            }
    
            if(allup && sum>=size*65 && sum<=size*90) {cout << "1" << endl;return true;}
            if(alllow && sum>=size*97 && sum<=size*122) {cout << "2" << endl;return true;}
            if(firstup && sum2>=(size-1)*97 && sum2<=(size-1)*122) {cout << "3" << endl;return true;}
            
            return false;
        }
    };
    

Log in to reply
 

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