C++ checking while traverse the array


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

  • 0
    D
            if (word.size() < 2) return true;
            bool first = isupper(word[0]);
            bool second = isupper(word[1]);
            if (!first && second) return false;
            for (int i = 2; i < word.size(); ++i) {
                if (islower(word[i]) && second || isupper(word[i]) && !second)
                    return false;
            }
            return true;    
    

Log in to reply
 

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