mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Update 0383.赎金信.md
This commit is contained in:
@ -136,7 +136,34 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user