Detect Capital
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.
Example 1:
Input: word = "USA"
Output: true
Example 2:
Input: word = "FlaG"
Output: false
Constraints:
1 <= word.length <= 100
word consists of lowercase and uppercase English letters.
思路一:按照题目意思,对长度大于 1 的字符串进行完全分类,只可能有四种情况,对这四种情况依次判断即可
public boolean detectCapitalUse(String word) {
if (word.length() == 1) return true;
char c1 = word.charAt(0);
char c2 = word.charAt(1);
if (Character.isUpperCase(c1) && Character.isUpperCase(c2)) {
for (int i = 2; i < word.length(); i++) {
if (Character.isLowerCase(word.charAt(i))) {
return false;
}
}
} else if ((Character.isLowerCase(c1) && Character.isLowerCase(c2)) ||
Character.isUpperCase(c1) && Character.isLowerCase(c2)) {
for (int i = 2; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i))) {
return false;
}
}
} else {
return false;
}
return true;
}
标签:return,charAt,Character,isUpperCase,520,easy,word,c1,leetcode
From: https://www.cnblogs.com/iyiluo/p/16870940.html