← All problems

520. Detect Capital

EasyOpen on LeetCodeProblem statement

Problem Statement

520. Detect Capital

Easy


We define the usage of capitals in a word to be right when one of the following cases holds:

Given a string word, return true if the usage of capitals in it is right.

 

Example 1:

Input: word = "USA"
Output: true

Example 2:

Input: word = "FlaG"
Output: false

 

Constraints:

C++ — alternative 1

Source file
class Solution {
public:
    bool detectCapitalUse(string word) {
        auto isCaps = [](char c)->bool{return (c>='A' && c<='Z');};
        if(!isCaps(word[0])){    // case 2 -> leetcode
            for(auto c:word)
                if (isCaps(c))
                    return false;
        }else{
            if(isCaps(word[1])){    // case 1 -> USA
                for(auto c:word)
                    if (!isCaps(c))
                        return false;
            }
            else for(auto c:word.substr(1))   // case 3 -> Google
                if (isCaps(c))
                    return false;
        }
        return true;
    }
};