|
| 1 | +Detect Capital |
| 2 | +Level-Easy |
| 3 | + |
| 4 | +We define the usage of capitals in a word to be right when one of the following cases holds: |
| 5 | +All letters in this word are capitals, like "USA". |
| 6 | +All letters in this word are not capitals, like "leetcode". |
| 7 | +Only the first letter in this word is capital, like "Google". |
| 8 | +Given a string word, return true if the usage of capitals in it is right. |
| 9 | +Example 1: |
| 10 | +Input: word = "USA" |
| 11 | +Output: true |
| 12 | +Example 2: |
| 13 | +Input: word = "FlaG" |
| 14 | +Output: false |
| 15 | +Constraints: |
| 16 | +1 <= word.length <= 100 |
| 17 | +word consists of lowercase and uppercase English letters. |
| 18 | + |
| 19 | +Time Complexity-o(n) |
| 20 | +Space Complexity-o(1) |
| 21 | + |
| 22 | +Java Solution |
| 23 | + |
| 24 | +class Solution { |
| 25 | + public boolean detectCapitalUse(String word) { |
| 26 | + if(word.length()>=2 && word.charAt(0)>=97 && word.charAt(0)<=123 && word.charAt(1)>=65 && word.charAt(1)<=91) |
| 27 | + return false; |
| 28 | + for(int i=1;i<word.length()-1;i++){ |
| 29 | + if(word.charAt(i)>=65 && word.charAt(i)<=91){ |
| 30 | + if(word.charAt(i+1)>=65 && word.charAt(i+1)<=91); |
| 31 | + else |
| 32 | + return false; |
| 33 | + } |
| 34 | + else{ |
| 35 | + if(word.charAt(i+1)>=97 && word.charAt(i+1)<=123); |
| 36 | + else |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + return true; |
| 41 | + } |
| 42 | +} |