반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 520 | Detect Capital | Python 본문
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
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 421 | Maximum XOR of Two Numbers in an Array | Python (0) | 2022.01.27 |
---|---|
[LeetCode] 1305 | All Elements in Two Binary Search Trees (0) | 2022.01.26 |
[백준] 3190번 | 뱀 | C++ (0) | 2022.01.20 |
[LeetCode] 875 | Koko Eating Bananas | Python (0) | 2022.01.20 |
[LeetCode] 142 | Linked List Cycle II | Python (0) | 2022.01.19 |
Comments