-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_capital.py
43 lines (36 loc) · 1.39 KB
/
detect_capital.py
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
'''
Given a word, you need to judge whether the usage of capitals in it is right or not.
We define the usage of capitals in a word to be right when one of the following cases holds:
All letters in this word are capitals, like "USA".
All letters in this word are not capitals, like "leetcode".
Only the first letter in this word is capital, like "Google".
Otherwise, we define that this word doesn't use capitals in a right way.
'''
class Solution:
def detectCapitalUse(self, word: str) -> bool:
s = word
all_caps = True
all_low = True
for i in range(len(s)):
if s[i].isupper() == False:
all_caps = False
break
for i in range(len(s)):
if s[i].islower() == False:
all_low = False
break
first_letter_cap = s[0].isupper()
the_rest = s[1:]
for i in range(len(the_rest)):
if the_rest[i].isupper():
first_letter_cap = False
break
if all_caps or all_low or first_letter_cap:
return True
return False
------------------------------------------------------------------------------
class Solution:
def detectCapitalUse(self, word: str) -> bool:
return (word == word.capitalize() or
word == word.upper() or
word == word.lower() )