another idea, C++


  • 0
    B
    class Solution {
    public:
        bool detectCapitalUse(string word) {
    
            // solution 1, for each char, if it is upper, then the condition is no lower case before,
            // if it is lower, then the condition is for i>=2 (from the third one, since for first one it can be lower and upper, for the second one, the first can be lower or upper), the previous char cannot be upper
            bool isLower = false;
            for(int i=0; i<word.size(); ++i){
                if(islower(word[i])){
                    isLower = true;
                    if(i>1 && isupper(word[i-1])) return false;
                }
                else if(isupper(word[i])){
                    if(isLower) return false;
                }
            }
            
            return true;
        }
    };

Log in to reply
 

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