Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- leetcode 풀기
- 릿코드 파이썬
- python priority queue
- 파이썬 알고리즘 풀기
- python 알고리즘
- leetcode풀기
- 파이썬릿코드풀기
- 코틀린기초
- 파이썬 알고리즘
- 상가수익률계산기
- 파이썬알고리즘풀기
- 릿코드풀기
- python 릿코드
- 알고리즘풀기
- 파이썬릿코드
- binary search
- python zip_longest
- 릿코드풀이
- python Leetcode
- 릿코드
- 알고리즘풀이
- python sorted
- 잇츠디모
- 파이썬 프로그래머스
- python xor
- 릿코드 풀기
- 파이썬알고리즘
- leetcode풀이
- 파이썬 릿코드
- LeetCode
Archives
- Today
- Total
소프트웨어에 대한 모든 것
520. 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.
Example 1:
Input: word = "USA"
Output: true
Example 2:
Input: word = "FlaG"
Output: false
Constraints:
- 1 <= word.length <= 100
- word consists of lowercase and uppercase English letters.
솔루션1)
- word의 대문자 수를 먼저 센 다음 조건에 맞는지 체크
class Solution:
def detectCapitalUse(self, word: str) -> bool:
capital_count = sum([1 for c in word if 'A' <= c <= 'Z'])
if capital_count == len(word):
# All letters in this word are capitals, like "USA".
return True
elif capital_count == 0:
# All letters in this word are not capitals, like "leetcode".
return True
elif capital_count == 1 and ('A' <= word[0] <= 'Z'):
# Only the first letter in this word is capital, like "Google".
return True
else:
return False

솔루션2)
- isupper(), islower(), istitle() 내장 함수 사용
class Solution:
def detectCapitalUse(self, word: str) -> bool:
return word.isupper() or word.islower() or word.istitle()

islower() Function in python checks whether the input string is in lowercase
isupper() Function in python checks whether the input string is in uppercase
istitle() Function in python checks whether the input string is in title case
출처 : https://www.datasciencemadesimple.com/lower-upper-title-function-python/
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
100. Same Tree (0) | 2023.01.10 |
---|---|
2233. Maximum Product After K Increments (0) | 2023.01.03 |
1834. Single-Threaded CPU (0) | 2023.01.02 |
290. Word Pattern (0) | 2023.01.01 |
1534. Count Good Triplets (0) | 2022.12.29 |