Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://leetcode.com/problems/minimum-window-substring/
- from collections import defaultdict, Counter
- class Solution:
- def minWindow(self, s: str, t: str) -> str:
- res = ''
- if not s or not t:
- return res
- d = dict(Counter(t))
- n = len(d)
- d_cur = defaultdict(int)
- idx_good = defaultdict(bool)
- idx_good_count = 0
- l, r = 0, -1
- while True:
- if idx_good_count < n:
- r += 1
- if r > len(s) - 1:
- break
- ch = s[r]
- if ch in d:
- d_cur[ch] += 1
- if d_cur[ch] >= d[ch] and not idx_good[ch]:
- idx_good[ch] = True
- idx_good_count += 1
- else:
- l += 1
- prev_ch = s[l - 1]
- if prev_ch in d:
- d_cur[prev_ch] -= 1
- if d_cur[prev_ch] < d[prev_ch] and idx_good[prev_ch]:
- idx_good[prev_ch] = False
- idx_good_count -= 1
- if idx_good_count == n:
- if (res and len(s[l:r + 1]) < len(res)) or not res:
- res = s[l:r + 1]
- return res
Add Comment
Please, Sign In to add comment