383.Ransom Note === [link](https://leetcode.com/problems/ransom-note/description/) 想法: 要剪出的字數目一定要比magazine上面的多,這樣才可以拼湊出來。 解法: 善用Python的counter功能,直接比較兩邊大小,判斷T或是F。 程式碼: ``` from collections import Counter class Solution(object): def canConstruct(self, ransomNote, magazine): """ :type ransomNote: str :type magazine: str :rtype: bool """ ct_ransom=Counter(ransomNote) ct_magazine=Counter(magazine) for i in ct_ransom: if ct_ransom[i]>ct_magazine[i]: return False return True ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up