iT邦幫忙

2024 iThome 鐵人賽

DAY 3
0
自我挑戰組

Leetcode 解題之旅:逐日攻克系列 第 3

每日一LeetCode(3)

  • 分享至 

  • xImage
  •  

383.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 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

程式碼(Python):

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

上一篇
每日一LeetCode(2)
下一篇
每日一LeetCode(4)
系列文
Leetcode 解題之旅:逐日攻克17
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言