HTMLify
The Smallest Window Substring
Views: 5 | Author: prakhardoneria
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | from collections import Counter class Solution: def minWindow(self, s, p): if not s or not p: return "" target_map = Counter(p) required = len(target_map) window_map = {} formed = 0 left = 0 min_len = float('inf') ans_start = 0 for right in range(len(s)): char = s[right] window_map[char] = window_map.get(char, 0) + 1 if char in target_map and window_map[char] == target_map[char]: formed += 1 while formed == required: if (right - left + 1) < min_len: min_len = right - left + 1 ans_start = left left_char = s[left] window_map[left_char] -= 1 if left_char in target_map and window_map[left_char] < target_map[left_char]: formed -= 1 left += 1 return "" if min_len == float('inf') else s[ans_start:ans_start + min_len] |