Skip to content

Instantly share code, notes, and snippets.

@Desolve
Last active July 2, 2020 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/d8fbf7b9d72a6292d129de62f4ac5add to your computer and use it in GitHub Desktop.
Save Desolve/d8fbf7b9d72a6292d129de62f4ac5add to your computer and use it in GitHub Desktop.
0383 Ransom Note
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
if len(ransomNote) > len(magazine):
return False
dic = {}
for ch in magazine:
if ch in dic:
dic[ch] += 1
else:
dic[ch] = 1
for ch in ransomNote:
if ch not in dic or dic[ch] == 0:
return False
else:
dic[ch] -= 1
return True
''' solutions from leetcode
from collections import Counter
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
counter = Counter(magazine)
for letter in ransomNote:
if counter[letter] > 0:
counter[letter] -= 1
else:
return False
return True
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
dict1 = {}
for key in ransomNote:
if key not in dict1:
dict1[key] = 1
else:
dict1[key] += 1
for key in dict1:
if dict1[key] > magazine.count(key):
return False
return True
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment