-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDetectCapital.py
More file actions
26 lines (19 loc) · 839 Bytes
/
DetectCapital.py
File metadata and controls
26 lines (19 loc) · 839 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
'''
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:
if len(word) < 2:
return True
lower = upper = 0
for letter in word:
if letter.isupper(): upper += 1
else: lower += 1
if (upper == 0) or (lower == 0) or (upper == 1 and word[0].isupper()):
return True
return False