Detection of capital letters

Keywords: Google network

Given a word, you need to determine whether it is capitalized correctly.

We define that the capitalization of a word is correct in the following cases:

All letters are capitalized, such as "USA".
All letters in a word are not capitalized, such as "leetcode".
If the word contains more than one letter, only the initials are capitalized, such as "Google".
Otherwise, we don't use capital letters correctly to define the word.

Example 1:

Input: "USA"
Output: True
Example 2:

Input: "FlaG"
Output: False
Note: Input is a non-empty word consisting of upper and lower Latin letters.

Source: LeetCode
Link: https://leetcode-cn.com/problems/detect-capital
Copyright belongs to the seizure network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

 

class Solution {
public:
    bool detectCapitalUse(string word) 
    {
        if(word.size() == 0)
            return false;
        if(word[0]>='a' && word[0]<='z')
        {
            for(int i = 1; i < word.size(); i++)
            {
                if(word[i]>='A' && word[i]<='Z')
                {
                    return false;
                }
            }
            return true;
        }
           
        
        int xiaoxie = 0;
        int daxie = 0;
        for(int i = 1; i < word.size(); i++)
        {
            if(word[i]>='a' && word[i]<='z')
                xiaoxie = 1;
            else if(word[i]>='A' && word[i]<='Z')
            {
                daxie = 1;
                if(xiaoxie == 1)
                    return false;
            }
        }
        
        if(xiaoxie == 1 && daxie == 0)
            return  true;
        else if(xiaoxie == 1 && daxie == 1)
            return false;
        else if(daxie == 1 && xiaoxie == 0)
            return true;
        else if(daxie == 0 && xiaoxie == 0)
            return true;
        return false;
    }
};

 

Posted by kristen on Mon, 30 Sep 2019 07:13:48 -0700