Home LeetCode - 520. Detect Capital
Post
Cancel

LeetCode - 520. Detect Capital

520. Detect Capital - easy

문제

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.

제한사항

입출력 예

1
2
3
4
Example 1:

Input: "USA"
Output: True
1
2
3
4
Example 2:

Input: "FlaG"
Output: False

풀이

  • String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    bool detectCapitalUse(string word) {
        int count = 0;
        bool first = false;
        
        for(auto i = 0 ; i < word.size() ; ++i){
            if('A' <= word[i] && word[i] <= 'Z'){
                if(i == 0)
                   first = true;
                
                ++count;
            }
        }
        
        
        if((count == 0) || (count == 1 && first) || (count == word.size()))
            return true;
        
        return false;
    }
};
This post is licensed under CC BY 4.0 by the author.