3ms C++ Solution


  • 0
    O
    class Solution {
    public:
        bool detectCapitalUse(string word) {
            bool isFirstCap = isupper(word[0]);
            bool hasNonCap = !isFirstCap;
            int capCount = isFirstCap ? 1 : 0;
            for (int i = 1; i < word.size(); i++)
            {
                if (isupper(word[i]))
                {
                    if (hasNonCap) return false;
                    capCount++;
                }
                else
                {
                    if (capCount > 1) return false;
                    hasNonCap = true;
                }
            }
            return true;
        }
    };
    

Log in to reply
 

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