Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 편스토랑
- 데이콘
- 코로나19
- github
- Real or Not? NLP with Disaster Tweets
- 편스토랑 우승상품
- 자연어처리
- dacon
- SW Expert Academy
- gs25
- ubuntu
- 우분투
- Git
- 금융문자분석경진대회
- Baekjoon
- 캐치카페
- 맥북
- 프로그래머스
- 프로그래머스 파이썬
- Kaggle
- 파이썬
- ChatGPT
- AI 경진대회
- 백준
- 더현대서울 맛집
- hackerrank
- Docker
- programmers
- leetcode
- PYTHON
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 383. Ransom Note (Python) 본문
728x90
반응형
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Example 1:
Input: ransomNote = "a", magazine = "b"
Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab"
Output: false
Example 3:
Input: ransomNote = "aa", magazine = "aab"
Output: true
Constraints:
- You may assume that both strings contain only lowercase letters.
Solution
from collections import Counter
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
answer = True
ran_list = list(ransomNote)
mag_list = list(magazine)
ran_cnt = dict(Counter(ran_list))
mag_cnt = dict(Counter(mag_list))
ran_keys = ran_cnt.keys()
mag_keys = mag_cnt.keys()
for r_key in ran_keys:
if r_key not in mag_keys:
answer = False
break
check = mag_cnt[r_key] - ran_cnt[r_key]
if check < 0:
answer = False
break
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 172. Factorial Trailing Zeroes (Python) (0) | 2020.07.21 |
---|---|
[leetCode] 137. Single Number II (Python) (2) | 2020.07.20 |
[leetCode] 9. Palindrome Number (Python) (0) | 2020.07.18 |
[leetCode] 819. Most Common Word (Python) (0) | 2020.07.17 |
[leetCode] 372. Super Pow (Python) (0) | 2020.07.15 |
Comments