작심삼일

[LeetCode] 520 | Detect Capital | Python 본문

스터디/코테

[LeetCode] 520 | Detect Capital | Python

yun_s 2022. 1. 24. 09:52
728x90
반응형

문제 링크: https://leetcode.com/problems/detect-capital/


문제

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.


조건

  • 1 <= word.length <= 100
  • word consists of lowercase and uppercase English letters.

내 풀이

파이썬 내장 함수 중 capitalize(), isupper(), islower()와 같이 좋은 것들이 있다.

아래 함수들을 이용해서 문제를 풀면 된다.

# capitalize: 첫 글자만 대문자로, 나머지는 소문자로 바꿔줌
a = 'aBcDe'
print(a.capicalize())	# 'Abcde'

# isupper: 모두 대문자면 True, 그렇지 않다면 False를 출력
a = 'aBcDe'
b = 'ABCDE'
print(a.isupper())	# False
print(b.isupper())	# True

# islower: 모두 소문자면 True, 그렇지 않다면 False를 출력
a = 'aBcDe'
b = 'abcde'
print(a.isupper())	# False
print(b.isupper())	# True

코드

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        if word.capitalize() == word or word.isupper() or word.islower():
            return True
        else:
            return False
728x90
반응형
Comments