LeetCode 솔루션 분류
[8/25] 383. Ransom Note
본문
383. Ransom Note
Easy
2273345Add to ListShareGiven 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
.
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:
1 <= ransomNote.length, magazine.length <= 105
ransomNote
andmagazine
consist of lowercase English letters.
Accepted
490,427
Submissions
868,093
관련자료
-
링크
댓글 1
학부유학생님의 댓글
- 익명
- 작성일
Runtime: 116 ms, faster than 30.44% of Python3 online submissions for Ransom Note.
Memory Usage: 14.3 MB, less than 20.43% of Python3 online submissions for Ransom Note.
Memory Usage: 14.3 MB, less than 20.43% of Python3 online submissions for Ransom Note.
import collections
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
rans_counter = collections.Counter(ransomNote)
mag_counter = collections.Counter(magazine)
for key in rans_counter:
if key not in mag_counter: return False
if rans_counter[key] > mag_counter[key]: return False
return True