We define the usage of capitals in a word to be right when one of the following cases holds:
- All letters in this word are capitals, like "USA".
- All letters in this word are not capitals, like "leetcode".
- Only the first letter in this word is capital, like "Google".
Given a string word, return true if the usage of capitals in it is right.
https://leetcode.com/problems/detect-capital/description/
Example 1:
Input: word = "USA"
Output: true
Example 2:
Input: word = "FlaG"
Output: false
class Solution {
public boolean detectCapitalUse(String word) {
if(word.length()==1) return true;
boolean res = true;
char[] c = word.toCharArray();
boolean first = Character.isUpperCase(c[0]);
boolean second = Character.isUpperCase(c[1]);
for(int i=2; i<c.length; i++){
if(first&&second&&Character.isUpperCase(c[i])){
}else if(first&&!second&&!Character.isUpperCase(c[i])){
}else if(!first&&!second&&!Character.isUpperCase(c[i])){
}else {
res = false;
break;
};
}
if(!first&&second) return false;
return res;
}
}