(Easy) Detect Capital -LeetCode
Description:
Given a word, you need to judge whether the usage of capitals in it is right or not.
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 capital, like "Google".
Example 1:
Input: "USA" Output: True
Example 2:
Input: "FlaG" Output: False
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
Accepted
90,478
Submissions
171,652
Solution:
class Solution { public boolean detectCapitalUse(String word) { //ASCII Code A-Z 65-90 // a-z 97-122 if(word==null||word.length()==0){ return true; } return (Lowercase_Check(word)||Upercase_Check(word)||First_Capital_Check(word)); } public boolean Lowercase_Check(String word){ for(int i = 0; i<word.length(); i++){ if(!(word.charAt(i) >='a' && word.charAt(i) <='z')){ return false; } } return true; } public boolean Upercase_Check(String word){ for(int i = 0; i<word.length(); i++){ if(!(word.charAt(i) >='A' && word.charAt(i) <='Z')){ return false; } } return true; } public boolean First_Capital_Check(String word){ if(word.charAt(0)>='A' && word.charAt(0)<= 'Z'){ for(int i = 1; i<word.length(); i++){ if(!( word.charAt(i) >='a' && word.charAt(i) <='z')){ return false; } } return true; } else{ return false; } } }