Given A word, you need to judge whether the usage of capitals in it are right or not.
We define the usage of capitals in a word to being right when one of the following cases holds:
- All letters in this word is capitals, like "USA".
- All letters in this word is not capitals, like "Leetcode".
- Only the first letter of this word was capital if it had more than one letter, like "Google".
Otherwise, we define that this word doesn ' t use capitals in a right-to-do.
Example 1:
Input: "USA" output:true
Example 2:
Input: "FlaG" Output:false
Note:the input is a non-empty word consisting of uppercase and lowercase Latin letters.
Public classSolution { Public BooleanDetectcapitaluse (String word) {if(Word = =NULL) return true; intupcnt = 0; for(inti=0; I<word.length (); i++) { CharCH =Word.charat (i); if(ch>= ' A ' && ch<= ' Z ') upcnt++; } if(Upcnt==word.length () | | upcnt==0) return true; Else { if(upcnt = = 1 && word.charat (0) >= ' A ' &&word.charat (0) <= ' Z ') return true; } return false; }}
LeetCode-520. Detect Capital