본문 바로가기
카테고리 없음

leet code - 383. Ransom Note

by monsangter 2023. 8. 13.

 

 

해쉬맵을 이용한 풀이.

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        dict1 = {}
        
        for i in magazine:
            if i not in dict1:
                dict1[i] = 1
            else:
                dict1[i] += 1
        
        for i in ransomNote:
            if i not in dict1 or dict1[i] == 0:
                return False
            dict1[i] -= 1
                
        return True

 

counter 객체를 이용한 풀이. 

 

class Solution(object):
    def canConstruct(self, ransomNote, magazine):
        st1, st2 = Counter(ransomNote), Counter(magazine)
        if st1 & st2 == st1:
            return True
        return False

댓글