C++ without any built-in function


  • 0

    Simply check for the three cases described in the problem statement.

        bool detectCapitalUse(string word) {
            bool firstCharUpper = false;
            int upperCount=0, sz=word.size();
            for (int i=0;i<sz;i++){
                if (i==0 && word[i]-'A' < 26 && word[i]-'A' >=0)
                    firstCharUpper = true;
                else  if (word[i]-'A' < 26 && word[i]-'A' >=0)
                        upperCount++;
            }
            if (firstCharUpper && (upperCount==0 || upperCount==sz-1))
                return true;
            else if (!firstCharUpper && upperCount==0)
                return true;
            return false;    
        }

Log in to reply
 

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