Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 1.38 KB

풀이_LC383.md

File metadata and controls

35 lines (25 loc) · 1.38 KB

🔥 LeetCode 383. Ransom Note

  • Date : 2021.09.19(일)
  • Time : 15분

Problem

  • Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise. Each letter in magazine can only be used once in ransomNote.

Constraints

  • 1 <= ransomNote.length, magazine.length <= 10^5
  • ransomNote and magazine consist of lowercase English letters.

Example

  • Input: ransomNote = "aa", magazine = "aab"
  • Output: true



풀이

    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        for i in ransomNote:
            if i in magazine:
                magazine = magazine.replace(i, '', 1)
            else:
                return False
        return True
        

: 이 문제는 ransomNote에 있는 문자열이 magazine에 다 쓰이는지 질문하는 문제! 그렇기 때문에 ransomNote의 문자열을 기준으로 하나씩 for 문을 돌아갔다. 그래서 magazine에 그 문자가 있다면 제일 앞에꺼 하나를 없애주었다. 이때 replace 함수를 사용하였다. 하지만 만약에 하나라도 없다면!!! 여기가 중요하다 하나라도 없으면 실패다.. 다 없애고 남는건 괜찮다. ransomNote 문자를 다 쓴것이기 때문에. 하지만 ranSomNote에는 있지만 magazine에는 없으면 안된다. 문제 이해가 제일 중요했던 것 같다.