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.
Topic meaning: Given a word, you need to determine whether the use of capital in it is correct. When one of the following conditions exists, we define the use of capital in the word as correct: all the letters in this word are capitalized, such as "USA". All the letters in this word are not uppercase, such as "Leetcode". Only the first letter in this word is uppercase if it has more than one letter, such as "Google". Otherwise, we define this word not to use uppercase in the correct way.
Idea: Find out the number of all uppercase letters CAPTIALCOUNT, compared to the string length, are lowercase letters, all uppercase, only the first letter is uppercase, all return true. The judging formula is:
Captialcount = = 0 | | Captialcount = = Word.length () | | (Captialcount = = 1 && (' Z '-word.charat (0) >= 0))
Letter ASCII decimal 0 00110000 489 00111001 57A 01000001 65Z 010 11010 90a 01100001 97z 01111010 122DEL (delete) 01111111 127
1 Public BooleanDetectcapitaluse (String word) {2 Char[] Words =Word.tochararray ();3 intCaptialcount = 0;4 for(Charletter:words) {5 if(' Z '-letter >= 0) captialcount++;6 }
7 returnCaptialcount = = 0 | | Captialcount = = Word.length () | | (Captialcount = = 1 && (' Z '-word.charat (0) >= 0)); 8}
520. Detect Capital