Advertisement

Minimum Window Substring - LeetCode 76 Solution

Minimum Window Substring - Complete Solution Guide

Minimum Window Substring is LeetCode problem 76, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t ( including duplicates ) is included in the window . If there is no such substring, return the empty string "" . The testcases will be generated such that the answer is unique . Example 1: Input: s = "ADOBECODEBANC", t = "ABC" Output: "BANC" Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t. Example 2: Input: s = "a", t = "a" O

Detailed Explanation

The problem asks us to find the smallest substring within a larger string 's' that contains all the characters of another string 't', including any duplicate characters present in 't'. The substring must contain at least one occurrence of each character in 't' with the required frequency. If such a substring exists, we return it; otherwise, we return an empty string. The problem guarantees that the answer is unique if it exists.

Solution Approach

The solution uses a sliding window technique combined with hash tables. We maintain two pointers, 'l' (left) and 'r' (right), which define the boundaries of our sliding window within the string 's'. We expand the window by incrementing 'r' and shrink it by incrementing 'l'. A hash table `dict_t` stores the required character counts from string 't', and another hash table `window_counts` tracks the character counts within the current window. We iterate through 's', expanding the window. When the window contains all characters in 't' with sufficient frequency, we try to shrink the window from the left, updating the minimum window found so far. The process continues until the right pointer reaches the end of the string 's'.

Step-by-Step Algorithm

  1. Step 1: Initialize two hash tables: `dict_t` to store the frequency of each character in string 't', and `window_counts` to track the frequency of characters in the current sliding window of 's'.
  2. Step 2: Initialize two pointers, 'l' and 'r', to 0, representing the left and right boundaries of the sliding window.
  3. Step 3: Initialize a variable `formed` to 0, to track how many characters in `dict_t` are fully satisfied within the current window.
  4. Step 4: Initialize a tuple or array `ans` to store the length of the minimum window found so far, and the start and end indices of the minimum window.
  5. Step 5: Iterate through string 's' with the right pointer 'r'.
  6. Step 6: For each character encountered at 's[r]', update its count in `window_counts`.
  7. Step 7: If the character at 's[r]' is present in `dict_t` and its count in `window_counts` matches its count in `dict_t`, increment `formed` by 1.
  8. Step 8: While `l` <= `r` and `formed` equals the number of unique characters in `dict_t` (i.e., `required`), it means the current window is valid. Then, shrink the window from the left.
  9. Step 9: Update the minimum window `ans` if the current window length is smaller than the previous minimum.
  10. Step 10: Decrement the count of the character at 's[l]' in `window_counts`.
  11. Step 11: If the character at 's[l]' is present in `dict_t` and its count in `window_counts` is now less than its count in `dict_t`, decrement `formed` by 1.
  12. Step 12: Increment the left pointer 'l' by 1.
  13. Step 13: Increment the right pointer 'r' by 1 to expand the window.
  14. Step 14: After iterating through the entire string 's', if a valid window was found, return the corresponding substring; otherwise, return an empty string.

Key Insights

  • Insight 1: Using a sliding window approach allows us to efficiently explore all possible substrings of 's' without redundant calculations.
  • Insight 2: Employing a hash table (or counter) is crucial for tracking the frequency of characters in 't' and within the current window of 's'. This enables constant-time lookups and updates.
  • Insight 3: Maintaining a separate counter for characters in the current window allows us to check if we've satisfied the character requirements defined by 't'.

Complexity Analysis

Time Complexity: O(s)

Space Complexity: O(t)

Topics

This problem involves: Hash Table, String, Sliding Window.

Companies

Asked at: Adobe, Agoda, Airbnb, Amazon, Apollo.io, Apple, Bloomberg, ByteDance, Fastenal, Infosys, LinkedIn, Lyft, MakeMyTrip, Meta, Microsoft, Nagarro, Oracle, PayPal, Salesforce, Snap, Snowflake, SoFi, TikTok, Uber, Walmart Labs, Yahoo, Yandex, Zeta, Zopsmart, thoughtspot.