엔지니어 게시판
LeetCode 솔루션 분류

[8/25] 383. Ransom Note

컨텐츠 정보

본문

383. Ransom Note
Easy
2273345Add to ListShare

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.

 

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 and magazine 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.
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
전체 404 / 9 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


알림 0