[520] Detection LeetCode uppercase

Title:
Given a word, you need to determine the correct word to use uppercase.

We define, in the following cases, uppercase word usage is correct:

All letters are capitalized, such as "USA".
All the letters in the word is not capitalized, such as "leetcode".
If the word contains more than one letter, only the first letter capitalized, such as "Google".
Otherwise, we define the word does not use capital letters correctly.

Example 1:

Input: "USA"
Output: True
Example 2:

Enter: "FlaG"
Output: False
Note: Input is non-empty by the word of uppercase and lowercase Latin letters.

public boolean detectCapitalUse(String word) {
        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++;
        }
        //三种情况 ,大写字母在首位、全是大写字母、无大写字母,return true;
        if(count == word.length() || count == 1 && word.charAt(0) >= 'A' && word.charAt(0) <='Z' || count==0)
            return true;
        return false;
    }
Published 55 original articles · won praise 14 · views 20000 +

Guess you like

Origin blog.csdn.net/qq422243639/article/details/103747301