1193. 检测大写的正确性
中文English
给定一个单词,你需要判断其中大写字母的使用是否正确。
当下列情况之一成立时,我们将单词中大写字母的用法定义为正确:
这个单词中的所有字母都是大写字母,如“USA”。
这个单词中的所有字母都不是大写字母,如“lintcode”。
如果它有多个字母,例如“Google”,那么这个单词中的第一个字母就是大写字母。
否则,我们定义该单词没有以正确的方式使用大写字母。
样例
样例 1:
输入: "USA"
输出: True
样例 2:
输入: "FlaG"
输出: False
注意事项
输入将是一个由大写和小写拉丁字母组成的非空单词。
分析:1.全部都是大写字母 2.除了首字母都是小写字母
public class Solution {
/**
* @param word: a string
* @return: return a boolean
*/
public boolean detectCapitalUse(String word) {
// write your code here
if (word == null || word == "") return false;
int count = 0;
for (int i = 0; i < word.length(); i++)
{
if (word.charAt(i) <= 'Z' && word.charAt(i) >= 'A')
count++;
}
if(count == word.length()||(count==1 && word.charAt(0)>='A' && word.charAt(0)<='Z')||count == 0)
return true;
return false;
}
}