for Robot Artificial Inteligence

70. Detect Capital(쉬움)

|

class Solution {
public:
    bool detectCapitalUse(string word) {
        int n = word.size();
        if(n==1)
            return true;
        int cap = 0;
        for(auto c : word)
        {
            if(c<97) // if c is less than 'a' means Capital, cap++
                cap++;
        }
        if(cap == 1)
            return word[0]<97; // check if the first character is captial;
        else
            return cap == 0 || cap == n; // All capital or all lowercase

    }
};

Comments