[LeetCode] Detect Capital

520. Detect Capital

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”.

Given a string word, return true if the usage of capitals in it is right.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool detectCapitalUse(string word) {
int up = 0;
for(auto w : word) if(isupper(w)) up += 1;
if(up == word.size()) return true;
if(up == 0) return true;
if(up == 1 and isupper(word[0])) return true;
return false;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/01/02/PS/LeetCode/detect-capital/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.