# [LeetCode] 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 if it has more than one letter, 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.

class Solution {
public:
bool detectCapitalUse(string word) {
int cnt = 0, n = word.size();
for (int i = 0; i < n; ++i) {
if (word[i] <= 'Z') ++cnt;
}
return cnt == 0 || cnt == n || (cnt == 1 && word[0] <= 'Z');
}
};

class Solution {
public:
bool detectCapitalUse(string word) {
int cnt = count_if(word.begin(), word.end(), [](char c){return c <= 'Z';});
return cnt == 0 || cnt == word.size() || (cnt == 1 && word[0] <= 'Z');
}
};

|
2月前
|

【Leetcode 程序员面试金典 02.08】 —— 环路检测 |双指针

32 0
|
2月前
leetcode：520. 检测大写字母
leetcode：520. 检测大写字母
22 0
|
2月前
|

leetcode-2013：检测正方形
leetcode-2013：检测正方形
34 0
|
2月前
|
Go
golang力扣leetcode 2013.检测正方形
golang力扣leetcode 2013.检测正方形
27 0
|
7月前
|

【leetcode报错】 leetcode格式问题解决：error: stray ‘\302’ in program [solution.c]
【leetcode报错】 leetcode格式问题解决：error: stray ‘\302’ in program [solution.c]
93 0
|
9月前
【Leetcode -509.斐波那契数 -520.检测大写字母】
【Leetcode -509.斐波那契数 -520.检测大写字母】
36 0
|
11月前
|

LeetCode-2013 检测正方形
LeetCode-2013 检测正方形
45 0
|

LeetCode算法小抄 -- 环检测算法 和 拓扑排序算法
LeetCode算法小抄 -- 环检测算法 和 拓扑排序算法
327 0
|
API Python

114 0
Leetcode_Python 520 检测大写字母

41 0