-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path520. Detect Capital.cpp
52 lines (52 loc) · 1.56 KB
/
520. Detect Capital.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public:
bool detectCapitalUse(string word) {
int n = word.size();
int count=0;
for(auto& c:word)
if(toupper(c)==c)
++count;
if(count==0 || count==n || (count==1 && toupper(word[0]) == word[0]))
return true;
return false;
}
bool detectCapitalUse1(string word) {
int n = word.size();
int caps = 0;
for(int i=0;i<n;++i)
{
if(isupper(word[i]))
++caps;
if(caps != i+1 && isupper(word[i]))
return false;
if(caps > 1 && islower(word[i]))
return false;
}
return true;
}
//another way
bool detectCapitalUse2(string word) {
int cnt=0;
int loc;
for(int i=0;i<word.length();i++){
if(word[i]>='A' && word[i]<='Z'){
cnt++;
loc = i;
}
}
if(cnt == word.length() || cnt == 0 || (cnt==1 && loc==0))
return true;
return false;
}
//another
bool detectCapitalUse3(string word) {
for(int i = 1; i < word.size(); i++){
if(isupper(word[1]) != isupper(word[i]) || islower(word[0]) && isupper(word[i]))
return false;
}
return true;
}
};