@@ -262,7 +262,7 @@ class Solution:
262
262
return res
263
263
```
264
264
265
- ###[ 最小覆盖子串] ( https://leetcode.cn/problems/minimum-window-substring/ )
265
+ ###[ 最小覆盖子串] ( https://leetcode.cn/problems/minimum-window-substring/ )
266
266
> 给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
267
267
>
268
268
> ** 注意** :
@@ -271,7 +271,7 @@ class Solution:
271
271
272
272
思路一:利用Counter计数器,比较方便但耗时非常多
273
273
274
- ** Python版本 **
274
+ ** Python版本一 **
275
275
``` python
276
276
from collections import Counter
277
277
class Solution :
@@ -299,7 +299,39 @@ class Solution:
299
299
return " "
300
300
```
301
301
302
+ ** Python版本二**
303
+ ``` python
304
+ from collections import Counter
305
+ class Solution :
306
+ def minWindow (self , s : str , t : str ) -> str :
307
+ ct = Counter()
308
+ for ch in t: ct[ch] += 1
309
+ need = len (t)
310
+ start = 0
311
+ res = len (s) + 1
312
+ wind = [- 1 , - 1 ]
313
+ for end in range (len (s)):
314
+ ch = s[end]
315
+ if ch in ct:
316
+ if ct[ch] > 0 : need -= 1
317
+ ct[ch] -= 1
318
+
319
+ while need == 0 and start <= end:
320
+ subL = end - start + 1
321
+ if res > subL:
322
+ res = subL
323
+ wind = [start, end+ 1 ]
324
+ ch = s[start]
325
+ if ch in ct:
326
+ if ct[ch] == 0 : need += 1
327
+ ct[ch] += 1
328
+ start = start + 1
329
+
330
+ if res != len (s) + 1 :
331
+ return s[wind[0 ]:wind[1 ]]
332
+ else : return " "
302
333
334
+ ```
303
335
304
336
305
337
@@ -337,4 +369,7 @@ vector<int> spiralOrder(vector<vector<int>>& matrix) {
337
369
- [ ] [移动零](https://leetcode.cn/problems/move-zeroes/)
338
370
- [ ] [有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/)
339
371
- [ ] [比较含退格的字符串](https://leetcode.cn/problems/backspace-string-compare/)
340
- - [ ] [spiral-matrix](https://leetcode-cn.com/problems/spiral-matrix/)
372
+ - [ ] [spiral-matrix](https://leetcode-cn.com/problems/spiral-matrix/)
373
+ - [ ] [长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/)
374
+ - [ ] [水果成篮](https://leetcode.cn/problems/fruit-into-baskets/)
375
+ - [ ] [最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/)
0 commit comments