simple and straightforward C++ solution


  • 0
    Z

    A simple and straightforward C++ solution. The return is true only if word[1] to word[n-1] is in the same case and only if the first letter is uppercase the following letters can be uppercase.

    class Solution {
    public:
        bool detectCapitalUse(string word) {
            int len = word.length();
            if(len < 2) return true;
            
            bool capBegin = word[0] < 'a';
            bool isCap = word[1] < 'a';
            if(!capBegin && isCap) return false;
            
            for(int i=2; i<len; i++){
                if(isCap != word[i] < 'a') return false;
            }
            
            return true;
        }
    };

Log in to reply
 

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