Minimum Window Substring

To solve this coding challenge, we need to find the minimum window substring in a given string \( s \) that includes all the characters in another string \( t \). The challenge is to ensure that our solution is efficient, ideally running in \( O(m + n) \) time, where \( m \) is the length of \( s \) and \( n \) is the length of \( t \). The problem can be approached using the "sliding window" technique along with a hash map to keep track of the character counts. Here’s a detailed explanation and methodology to solve the given problem:
Explanation
  1. Initial Setup :
    • If either string \( s \) or \( t \) is empty, return an empty string as no window can satisfy the requirements.
  2. Character Count Map :
    • Create a dictionary (hash map) called
      char_count
      to count the frequency of each character in \( t \). This map will help us check if a window has all the characters required by \( t \).
  3. Two Pointers :
    • Initiate two pointers,
      left
      and
      right
      . The
      right
      pointer expands the window, and the
      left
      pointer contracts the window when we have a valid window containing all characters of \( t \).
  4. Sliding Window :
    • Move the
      right
      pointer to expand the window until it contains all characters from \( t \).
    • Once all characters are included in the current window, try to shrink the window from the left to find the minimum length window. Update the result whenever a smaller valid window is found.
    • During window contraction, if any character is removed from the window and the window no longer contains all characters from \( t \), move the
      left
      pointer to re-expand the window by moving the
      right
      pointer again.
  5. Update Result :
    • Keep track of the minimum window length found and update the starting position of this window.
  6. Return the Result :
    • If no valid window is found, return an empty string. Otherwise, return the substring corresponding to the minimum window found.
Detailed Steps in Pseudocode
                                            
// Function to find the minimum window substring in s that contains all characters of t
function findMinWindowSubstring(s, t):
    // If either string s or t is empty, return empty string
    if s is empty or t is empty:
        return ""

    // Dictionary to store the character count of string t
    char_count = {}
    // Populate char_count map with characters from t
    for char in t:
        if char in char_count:
            char_count[char] += 1
        else:
            char_count[char] = 1

    // Initialize variables for the sliding window technique
    left = 0
    min_left = 0
    min_len = infinity  // Min length is initially set to infinity
    count = length of t  // Number of characters we need to include in the window

    // Move the right pointer from 0 to the end of the string s
    for right in range(0, length of s):
        // If s[right] is a character we need, decrease the count in char_count
        if s[right] in char_count:
            char_count[s[right]] -= 1
            // Decrease the count only if the character count in the map is still 0 or above
            if char_count[s[right]] >= 0:
                count -= 1

        // When we have a valid window (containing all characters from t)
        while count == 0:
            // Check if the current window is smaller than the minimum found
            if right - left + 1 < min_len:
                min_left = left  // Update the starting index of the minimum window
                min_len = right - left + 1  // Update the minimum length

            // Try to contract the window from the left
            if s[left] in char_count:
                char_count[s[left]] += 1  // Need to include this character for future windows
                // If char count goes above 0, it's no longer a valid window
                if char_count[s[left]] > 0:
                    count += 1

            left += 1  // Move the left pointer to the right

    // Return the minimum window or empty string if no valid window was found
    if min_len == infinity:
        return ""
    else:
        return substring of s from min_left to min_left + min_len

                                        
In this pseudocode:
  • The dictionary
    char_count
    keeps track of the required characters and their frequencies.
  • Two pointers,
    left
    and
    right
    , are used to expand and contract the sliding window.
  • When
    count
    is zero, it indicates a valid window containing all characters from \( t \).
  • min_left
    and
    min_len
    are used to store the start position and length of the smallest valid window found.
By following these detailed steps, the goal of finding the minimum window substring is achieved efficiently.