# Detect Capital LeetCode Solution

·1 min
leetcode
Author
Mayukh Datta

### Problem statement: #

Given a word, you need to judge whether the usage of capitals in it is right or not.

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

1. All letters in this word are capitals, like “USA”.
2. All letters in this word are not capitals, like “leetcode”.
3. Only the first letter in this word is capital, like “Google”.

Otherwise, we define that this word doesn’t use capitals in a right way.

Examples:

Input: “USA” Output: True

Input: “FlaG” Output: False

Input: “FFFFFf” Output: False

Input: “fffffF” Output: False

### Solution in C++ #

class Solution { public: bool detectCapitalUse(string word) { int i = 0; int len = word.length();

``````    // all capital letters
if(isupper(word\[0\]) && isupper(word\[1\])) {
for(i=2; i<len; i++) {
if(islower(word\[i\])) {
return false;
}
}
}else{
// ignore the first letter
// if there is any capital letter
// in positions other than 0
// return false
for(i=1; i<len; i++) {
if(isupper(word\[i\])) {
return false;
}
}
}

return true;
}
``````

};

Another solution: It can be matched using a regex `[A-Z]*|.[a-z]*`