[LeetCode]#520. Detect Capital

Fatboy Slim
1 min readMar 19, 2020

--

Environment: Python 3.7

Key technique: .isupper, islower, slice

Example 1:

Input: "USA"
Output: True

Example 2:

Input: "FlaG"
Output: False

Analysis:

There are three rules in this problem.

  1. All letters in this word are capitals, like “USA”.
  2. All letters in this word are not capitals, like “leetcode”.
  3. Only the first letter in this word is capital, like “Google

Solution:

class Solution:
def detectCapitalUse(self, word: str) -> bool:
if word.isupper()==True:
return True
elif word.islower()==True:
return True
elif word[0].isupper() and word[1:].islower() ==True:
return True
else:
return False

Submitted result:

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet