-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_1 Detect_Capital.cpp
67 lines (54 loc) · 1.52 KB
/
Day_1 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//Detect Capital
//Solution 1 using conditions
//Time Complexity O(n)
//Space Complexity O(1)
class Solution {
public:
bool detectCapitalUse(string word) {
int n=word.size();
if(isupper(word[0]) && isupper(word[1])) //USA //USbc
{
for(int i=2;i<n;i++)
if(!isupper(word[i]))
return false;
}
else{ // Google leetcode //leeTcode
for(int i=1;i<n;i++)
if(!islower(word[i]))
return false;
}
return true;
}
};
//Solution 2 using regex
//Time Complexity O(2^n)
//Space Complexity O(1)
class Solution {
public:
bool detectCapitalUse(string word) {
// all captial or first capital all other small or all small
regex reg("[A-Z]*|[A-Z][a-z]*|[a-z]*");
return regex_match(word,reg);
}
};
//Solution 3 using capital letter count
//Time Complexity O(n)
//Space Complexity O(1)
class Solution {
public:
bool detectCapitalUse(string word) {
// all captial or first capital all other small or all small
int n=word.size();
int capital_count=0;
for(int i=0;i<n;i++)
{
if(word[i]<='Z')
capital_count++;
}
if(word[0]<='Z'&& (capital_count==1 || capital_count==n))
return true;
if(word[0]>='a' && capital_count==0)
return true;
return false;
}
};