Stupid 12ms C++ Condition Checking


  • 0
    class Solution {
    public:
        bool is_all_lower(string word) {
            for (auto& ch: word) {
                if(isupper(ch)) return false;
            }
            return true;
        }
        
        bool is_all_upper(string word) {
            for (auto& ch: word) {
                if(islower(ch)) return false;
            }
            return true;
        }
        
        bool detectCapitalUse(string word) {
            if (word.empty()) return false;
            if (word.size() == 1) return true;
            // else case size > 1
            if (isupper(word[0])) {
                return is_all_lower(word.substr(1, word.size()-1)) || is_all_upper(word.substr(1, word.size()-1));
            } else {
                return is_all_lower(word.substr(1, word.size()-1));
            }
            
        }
    };
    

Log in to reply
 

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