반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 383 | Ransom Note | Python 본문
728x90
반응형
문제 링크: https://leetcode.com/problems/ransom-note/
문제
Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.
조건
- 1 <= ransomNote.length, magazine.length <= $10^5$
- ransomNote and magazine consist of lowercase English letters.
내 풀이
magazine에서 ransomNote 안에 있는 한 글자씩 제거해보면서 문제의 조건에 해당하는지 확인한다.
코드
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
for r in ransomNote:
if r in magazine:
magazine = magazine.replace(r, '', 1)
else:
return False
return True
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 48 | Rotate Image | Python (0) | 2022.08.30 |
---|---|
[LeetCode] 200 | Number of Islands | Python (0) | 2022.08.29 |
[LeetCode] 326 | Power of Three | Python (0) | 2022.08.24 |
[LeetCode] 1338 | Reduce Array size to The Half | Python (0) | 2022.08.18 |
[LeetCode] 804 | Unique Morse Code Words | Python (0) | 2022.08.17 |
Comments