nathanwailes

LeetCode 76 - Minimum Window Substring - 2022.12.27 solution

Dec 26th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. class Solution:
  2.     def minWindow(self, s: str, t: str) -> str:
  3.         # Maintain a variable of the minimum window substring found so far
  4.         # Maintain a dict count of t
  5.         # Maintain a dict count of s in the current window
  6.         # Maintain a variable 'matches' that tracks how many of the characters in t
  7.         # have all of their necessary matches in s.
  8.         # First create the dict count of t.  Then iterate through s with the first
  9.         # pointer, updating the dict count of s and the 'matches' variable as you go.
  10.         # For each new character, you want to update the dict count of s for that
  11.         # character, then check how it compares to the number of characters needed to
  12.         # match s, and if it matches, increment the 'matches' variable.
  13.         # Once you've hit the needed number of matches, start advancing the left
  14.         # pointer to see how small you can get the string.  Once you hit the point
  15.         # where you no longer have enough matches, then start advancing the right
  16.         # pointer again.
  17.         from collections import defaultdict
  18.         minimum_substring = ''
  19.  
  20.         # Create the dict count of characters in t
  21.         t_dict = defaultdict(int)
  22.         for char in t:
  23.             t_dict[char] += 1
  24.  
  25.         remaining_characters_to_match = len(t_dict.keys())
  26.  
  27.         s_dict = defaultdict(int)
  28.        
  29.         l = 0
  30.         r = 0
  31.         # we want to keep going as long as the right pointer hasn't hit the end
  32.         # and the current window in s has all the needed characters for t.
  33.         while r < len(s) or remaining_characters_to_match == 0:
  34.  
  35.             # Increment the right pointer
  36.             while r < len(s) and remaining_characters_to_match > 0:
  37.                 current_char_in_s = s[r]
  38.                 s_dict[current_char_in_s] += 1 # A: 1
  39.  
  40.                 if s_dict[current_char_in_s] == t_dict[current_char_in_s]:
  41.                     remaining_characters_to_match -= 1
  42.                
  43.                 r += 1
  44.            
  45.             # Increment the left pointer
  46.             while l <= r and remaining_characters_to_match == 0:
  47.                
  48.                 # Update the min substring if necessary
  49.                 if not minimum_substring or (r - l) < len(minimum_substring):
  50.                     minimum_substring = s[l:r+1]
  51.  
  52.                 char_to_remove = s[l]
  53.                 s_dict[char_to_remove] -= 1
  54.  
  55.                 if s_dict[char_to_remove] == t_dict[char_to_remove] - 1:
  56.                     remaining_characters_to_match += 1
  57.  
  58.                 l += 1
  59.            
  60.         return minimum_substring
Advertisement
Add Comment
Please, Sign In to add comment