Detect Capital - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Friday 7 August 2020

Detect Capital


Detect Capital


Detect Capital

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.
Input: "USA"
Output: True

Input: "FlaG"
Output: False

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