(Easy) Detect Capital -LeetCode

泪湿孤枕 提交于 2019-11-28 05:39:22

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:

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital, like "Google".
Otherwise, we define that this word doesn't use capitals in a right way.

 

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;
        }
        
        
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!