nathanwailes

LeetCode 76 - Minimum Window Substring - 2023.10.16 solution

Oct 16th, 2023
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. class Solution:
  2.     def minWindow(self, s: str, t: str) -> str:
  3.         # This is a dict of t chars to their counts.
  4.         t_counts = get_t_counts(t)
  5.  
  6.         # A set telling us which chars we need to still get enough of in
  7.         # our subwindow.
  8.         t_chars_we_still_need_to_match = {char for char in t_counts.keys()}
  9.  
  10.         # This is a list of all the indices that contain characters in t.
  11.         s_window_entries = []
  12.  
  13.         # This is a dict of the characters in t to their number in the
  14.         # current window in s.
  15.         s_window_counts = defaultdict(int)
  16.  
  17.         minimum_window_substring = ""
  18.  
  19.         for i, c in enumerate(s):
  20.             if c not in t_counts:
  21.                 continue
  22.            
  23.             s_window_counts[c] += 1
  24.             s_window_entries.append(i)
  25.        
  26.             if s_window_counts[c] < t_counts[c]:
  27.                 t_chars_we_still_need_to_match.add(c)
  28.             elif s_window_counts[c] >=  t_counts[c] and c in t_chars_we_still_need_to_match:
  29.                 t_chars_we_still_need_to_match.remove(c)
  30.            
  31.             # Check if we should handle a matching subwindow
  32.             if len(t_chars_we_still_need_to_match) == 0:
  33.                 # Check if we can remove chars from the front of the
  34.                 # subwindow
  35.                 while True:
  36.                     if len(s_window_entries) == 0:
  37.                         break
  38.                     front_char_index = s_window_entries[0]
  39.                     front_char = s[front_char_index]
  40.                     if s_window_counts[front_char] > t_counts[front_char]:
  41.                         # Ditch this front char
  42.                         s_window_counts[front_char] -= 1
  43.                         s_window_entries.pop(0)
  44.                     else:
  45.                         break
  46.                
  47.                 # Calculate the subwindow
  48.                 current_subwindow_length = s_window_entries[-1] - s_window_entries[0] + 1
  49.  
  50.                 # If the new subwindow is smaller, use it.
  51.                 if not minimum_window_substring or current_subwindow_length < len(minimum_window_substring):
  52.                     # Update the minimum window_substring
  53.                     minimum_window_substring = s[s_window_entries[0]:s_window_entries[-1]+1]
  54.  
  55.                 # Pop off the first character in the subwindow so we can start looking for a better subwindow
  56.                 front_char = s[s_window_entries[0]]
  57.                 s_window_counts[front_char] -= 1
  58.                 s_window_entries.pop(0)
  59.                 if s_window_counts[front_char] < t_counts[front_char]:
  60.                     t_chars_we_still_need_to_match.add(front_char)
  61.         return minimum_window_substring
  62.  
  63. def get_t_counts(t):
  64.     t_counts = defaultdict(int)
  65.     for c in t:
  66.         t_counts[c] += 1
  67.     return t_counts
  68.  
Advertisement
Add Comment
Please, Sign In to add comment