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.
Next challenges: Compare Version Numbers Basic Calculator II Ransom Note
Idea: In Java to determine whether 2 strings are equal cannot use "= =", to use "equal". Character comparisons can be made with "= =".
Code:
1 Public class Solution {2 Public Boolean Detectcapitaluse (String word) {3 return Word = = word.touppercase () | | Word = = Word.tolowercase () | | (Word.length () > 1 && character.touppercase (Word.charat (0)) = = Word.charat (0) && word.substring (1). toLowerCase (). Equals (word.substring (1))); 4 }5 }
Leetcode 520. Detect Capital