작심삼일

[LeetCode] 383 | Ransom Note | Python 본문

스터디/코테

[LeetCode] 383 | Ransom Note | Python

yun_s 2022. 8. 25. 18:52
728x90
반응형

문제 링크: https://leetcode.com/problems/ransom-note/

 

Ransom Note - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


문제

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
반응형
Comments