We define the usage of capitals in a word to be right when one of the following cases holds:
- All letters in this word are capitals, like
"USA"
. - All letters in this word are not capitals, like
"leetcode"
. - Only the first letter in this word is capitalized, like
"Google"
.
Given a string “word”, return true
if the usage of capitals in it is right.
https://leetcode.com/problems/detect-capital/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Solution { public boolean detectCapitalUse(String word) { int count = 0; char[] cArr = word.toCharArray(); for(char c: cArr){ //is upper case if(c >= 65 && c <= 90){ count++; } } //no upper case character found if(count==0){ return true; } //1 uppercase character found //it should be the first character otherwise return false else if(count == 1){ return Character.isUpperCase(cArr[0]); } //For it to be valid all characters should be uppercase return count == word.length(); } } |