Easy to understand, non regex, c++ solution


  • 0
    H
    class Solution {
    public:
        bool detectCapitalUse(string word) {
            
            if (word.size() == 0 || word.size() == 1) return true;
            
            for(int i = 2; i < word.size(); i++)
                if (isCapital(word[i]) != isCapital(word[i-1])) return false;
            
            return (isCapital(word[0]) == isCapital(word[1]) || (isCapital(word[0]) == true && isCapital(word[1]) == false));
            
        }
        
    private:
        bool isCapital(char in) {
            return (in -'a' < 26 && in - 'a' >= 0) ? false:true;
        }
    };
    

Log in to reply
 

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