Last active
July 2, 2020 04:01
-
-
Save Desolve/d8fbf7b9d72a6292d129de62f4ac5add to your computer and use it in GitHub Desktop.
0383 Ransom Note
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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