We define the usage of capitals in a word to be right when one of the following cases holds:
"USA"."leetcode"."Google".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:
1 <= word.length <= 100word consists of lowercase and uppercase English letters.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;
}
};class Solution {
public:
bool detectCapitalUse(string word) {
int c=0;
for (int i=0; i<word.length(); i++){
if(word[i]>='A' && word[i]<='Z')
c++;
}
if(c==word.length()||c==0)
return true;
else if(c==1 && word[0]>='A' && word[0]<='Z')
return true;
return false;
}
};