Detect Capital LeetCode solution in C++

1
class Solution {
2
public:
3
bool detectCapitalUse(string word) {
4
int n = word.size();
5

6
// if the first letter of the string is lower-case
7
if (islower(word[0])) {
8
int c = 0;
9
for (int i = 0; i < n; i++) {
10
if (islower(word[i])) {
11
c++;
12
}
13
}
14

15
// total lower-case count must be equal to the size of the string
16
if (c == n) {
17
return true;
18
}
19
return false;
20
}
21

22
// if the first letter of the string is upper-case.
23
else {
24
int c = 0;
25
for (int i = 0; i < n; i++) {
26
if (isupper(word[i])) {
27
c++;
28
}
29
}
30

31
// count of total upper-case letters must be equal to 1 or to the size of
32
// the string.
33
if (c == 1 or c == n) {
34
return true;
35
}
36

37
// in all other cases, return false.
38
return false;
39
}
40
}
41
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0