[LeetCode]520. Detect Capital
题目要求
思路
统计大小写数目,和单词数目比较,全大写或全小写,只有一个大写时检测是否首字母
代码
class Solution {
public:
bool detectCapitalUse(string word) {
unordered_map<int, int> detect;
for (char p : word){
if (p >= 'a' && p <= 'z') {
++detect[0];
}
else{
++detect[1];
}
}
if (detect[0] == word.size() || detect[1] == word.size() || (detect[1] == 1 && word[0] >= 'A' && word[0] <= 'Z')){
return true;
}
return false;
}
};