@@ -142,94 +142,70 @@ class Solution {
142
142
143
143
```
144
144
145
- Python写法一(使用数组作为哈希表):
146
-
145
+ (版本一)使用数组
147
146
``` python
148
147
class Solution :
149
148
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 ))
163
156
```
164
157
165
- Python写法二(使用defaultdict):
158
+ (版本二)使用defaultdict
166
159
167
160
``` python
161
+ from collections import defaultdict
162
+
168
163
class Solution :
169
164
def canConstruct (self , ransomNote : str , magazine : str ) -> bool :
170
165
171
- from collections import defaultdict
172
-
173
166
hashmap = defaultdict(int )
174
167
175
168
for x in magazine:
176
169
hashmap[x] += 1
177
170
178
171
for x in ransomNote:
179
172
value = hashmap.get(x)
180
- if value is None or value == 0 :
173
+ if not value or not value :
181
174
return False
182
175
else :
183
176
hashmap[x] -= 1
184
177
185
178
return True
186
179
```
187
-
188
- Python写法三:
180
+ (版本三)使用字典
189
181
190
182
``` 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 :
214
190
return False
215
-
191
+ counts[c] -= 1
216
192
return True
217
193
```
194
+ (版本四)使用Counter
218
195
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
220
204
221
205
``` python
222
206
class Solution :
223
207
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))
233
209
```
234
210
235
211
Go:
0 commit comments