forked from Aadi71/Data-Structures-and-Algorithms-with-Aadi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect-capital.cpp
42 lines (40 loc) · 1.02 KB
/
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
// https://leetcode.com/problems/detect-capital/
// Optimized Approach
class Solution {
public:
bool detectCapitalUse(string word) {
int small = 0;
for(char i: word) if(islower(i)) small++;
if(small == 0 || (small == word.length() - 1 && isupper(word[0])) || small == word.length()) return true;
return false;
}
};
// Brute Force
class Solution {
public:
bool detectCapitalUse(string word) {
int n = word.length();
int i = 1;
if(isupper(word[0])){
while(i < n){
if(isupper(word[i])) i++;
else break;
}
if(i == n) return true;
i = 1;
while(i < n){
if(islower(word[i])) i++;
else break;
}
if(i == n) return true;
}
else{
while(i < n){
if(islower(word[i])) i++;
else break;
}
if(i == n) return true;
}
return false;
}
};