My simple 6ms C++ Solution. Beat 80.21%


  • 0
    S
    class Solution {
    public:
        bool detectCapitalUse(string word) {
            bool result = true;
            if(isupper(word[0])){
                if(isupper(word[1])){
                    int i = 2;
                    while(i < word.length()){
                        if(islower(word[i])) return false;
                        i++;
                    }
                }
                else{
                    int i = 2;
                    while(i < word.length()){
                        if(isupper(word[i])) return false;
                        i++;
                    }
                }
            }
            else{
                int i = 1;
                while(i < word.length()){
                    if(isupper(word[i])) return false;
                    i++;
                }
            }
            return result;
        }
    };

  • 0
    C

    you did not consider empty string


Log in to reply
 

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