Skip to content

Commit 38bc702

Browse files
Update 0383.赎金信.md
1 parent 5cff41a commit 38bc702

File tree

1 file changed

+31
-55
lines changed

1 file changed

+31
-55
lines changed

problems/0383.赎金信.md

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -142,94 +142,70 @@ class Solution {
142142

143143
```
144144

145-
Python写法一(使用数组作为哈希表):
146-
145+
(版本一)使用数组
147146
```python
148147
class Solution:
149148
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
150-
151-
arr = [0] * 26
152-
153-
for x in magazine: # 记录 magazine里各个字符出现次数
154-
arr[ord(x) - ord('a')] += 1
155-
156-
for x in ransomNote: # 在arr里对应的字符个数做--操作
157-
if arr[ord(x) - ord('a')] == 0: # 如果没有出现过直接返回
158-
return False
159-
else:
160-
arr[ord(x) - ord('a')] -= 1
161-
162-
return True
149+
ransom_count = [0] * 26
150+
magazine_count = [0] * 26
151+
for c in ransomNote:
152+
ransom_count[ord(c) - ord('a')] += 1
153+
for c in magazine:
154+
magazine_count[ord(c) - ord('a')] += 1
155+
return all(ransom_count[i] <= magazine_count[i] for i in range(26))
163156
```
164157

165-
Python写法二(使用defaultdict):
158+
(版本二)使用defaultdict
166159

167160
```python
161+
from collections import defaultdict
162+
168163
class Solution:
169164
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
170165

171-
from collections import defaultdict
172-
173166
hashmap = defaultdict(int)
174167

175168
for x in magazine:
176169
hashmap[x] += 1
177170

178171
for x in ransomNote:
179172
value = hashmap.get(x)
180-
if value is None or value == 0:
173+
if not value or not value:
181174
return False
182175
else:
183176
hashmap[x] -= 1
184177

185178
return True
186179
```
187-
188-
Python写法三:
180+
(版本三)使用字典
189181

190182
```python
191-
class Solution(object):
192-
def canConstruct(self, ransomNote, magazine):
193-
"""
194-
:type ransomNote: str
195-
:type magazine: str
196-
:rtype: bool
197-
"""
198-
199-
# use a dict to store the number of letter occurance in ransomNote
200-
hashmap = dict()
201-
for s in ransomNote:
202-
if s in hashmap:
203-
hashmap[s] += 1
204-
else:
205-
hashmap[s] = 1
206-
207-
# check if the letter we need can be found in magazine
208-
for l in magazine:
209-
if l in hashmap:
210-
hashmap[l] -= 1
211-
212-
for key in hashmap:
213-
if hashmap[key] > 0:
183+
class Solution:
184+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
185+
counts = {}
186+
for c in magazine:
187+
counts[c] = counts.get(c, 0) + 1
188+
for c in ransomNote:
189+
if c not in counts or counts[c] == 0:
214190
return False
215-
191+
counts[c] -= 1
216192
return True
217193
```
194+
(版本四)使用Counter
218195

219-
Python写法四:
196+
```python
197+
from collections import Counter
198+
199+
class Solution:
200+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
201+
return not Counter(ransomNote) - Counter(magazine)
202+
```
203+
(版本五)使用count
220204

221205
```python
222206
class Solution:
223207
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
224-
c1 = collections.Counter(ransomNote)
225-
c2 = collections.Counter(magazine)
226-
x = c1 - c2
227-
#x只保留值大于0的符号,当c1里面的符号个数小于c2时,不会被保留
228-
#所以x只保留下了,magazine不能表达的
229-
if(len(x)==0):
230-
return True
231-
else:
232-
return False
208+
return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote))
233209
```
234210

235211
Go:

0 commit comments

Comments
 (0)