-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_capital.py
More file actions
34 lines (29 loc) · 896 Bytes
/
detect_capital.py
File metadata and controls
34 lines (29 loc) · 896 Bytes
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
class Solution(object):
def detectCapitalUse(self, word):
"""
:type word: str
:rtype: bool
"""
'''
Time Complexity: O(n)
Space Complexity: O(1)
'''
if len(word) == 1:
return True
is_first_cap = word[0].isupper()
is_second_cap = word[1].isupper()
if is_first_cap and is_second_cap:
for char in word:
if char.islower():
return False
elif is_first_cap and not is_second_cap:
for char in word[1:]:
if char.isupper():
return False
elif not is_first_cap and is_second_cap:
return False
elif not is_first_cap and not is_second_cap:
for char in word[2:]:
if char.isupper():
return False
return True