From 830381313abb85357f180f6315e0e1c28c63c01d Mon Sep 17 00:00:00 2001 From: tw2665 <55668073+tw2665@users.noreply.github.com> Date: Tue, 18 May 2021 15:53:09 -0400 Subject: [PATCH] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91=E4=BF=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 2f7ae4d8..527945fa 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -136,7 +136,34 @@ class Solution { ``` Python: - +``` +class Solution(object): + def canConstruct(self, ransomNote, magazine): + """ + :type ransomNote: str + :type magazine: str + :rtype: bool + """ + + # use a dict to store the number of letter occurance in ransomNote + hashmap = dict() + for s in ransomNote: + if s in hashmap: + hashmap[s] += 1 + else: + hashmap[s] = 1 + + # check if the letter we need can be found in magazine + for l in magazine: + if l in hashmap: + hashmap[l] -= 1 + + for key in hashmap: + if hashmap[key] > 0: + return False + + return True +``` Go: