Coding Test/Python

[LeetCode] 383. Ransom Note (easy, python)

lim.dev 2024. 1. 15. 22:36

https://leetcode.com/problems/ransom-note/?envType=study-plan-v2&envId=top-interview-150

 

Ransom Note - LeetCode

Can you solve this real interview question? 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 ranso

leetcode.com

 

전체 코드

from collections import Counter

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        counter = Counter(magazine)

        for r in ransomNote:
            if not counter[r]: return False
            elif counter[r] > 0: counter.subtract(r)
  
        return True