Leetcode 520 Detect Capital
#include <string>
using namespace std;
class Solution {
public:
bool detectCapitalUse(string word) {
if (word.length() <= 1)
return true;
bool sign = true;
if (isupper(word[0])){
int upperCount = 0;
for (int i = 1; i < word.length(); i++){
if (isupper(word[i]))
upperCount++;
}
if (upperCount != 0 && upperCount != word.length() - 1)
sign = false;
}
else if (islower(word[0])){
for (int i = 0; i < word.length(); i++){
if (isupper(word[i]))
sign = false;
}
}
return sign;
}
};
class Solution {
public:
bool detectCapitalUse(string word) {
int upperCount =0;
for (int i = 0; i < word.length(); i++){
if (word[i] >= 'A' && word[i] <= 'Z')
upperCount++;
}
if (word[0] >= 'A' && word[0] <= 'Z'){
if (upperCount == 1 || upperCount == word.length())
return true;
}
else if (upperCount == 0)
return true;
return false;
}
};