-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2 JAN - LEET - 520. Detect Capital.py
43 lines (35 loc) · 1.23 KB
/
2 JAN - LEET - 520. 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
#Leetcode question solution - 02/01/2023
'''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".
Given a string word, return true if the usage of capitals in it is right.'''
class Solution:
def detectCapitalUse(self, word: str) -> bool:
n = len(word)
match1, match2, match3 = True, True, True
# case 1: All capital
for i in range(n):
if not word[i].isupper():
match1 = False
break
if match1:
return True
# case 2: All not capital
for i in range(n):
if word[i].isupper():
match2 = False
break
if match2:
return True
# case 3: All not capital except first
if not word[0].isupper():
match3 = False
if match3:
for i in range(1, n):
if word[i].isupper():
match3 = False
if match3:
return True
# if not matching
return False