Minimum Window Substring

Program for Smallest Substring in a string that contains all the characters of another string Problem

You are given two strings, s and t. Your task is to find the smallest substring in s that contains all the characters of t, including their respective frequencies (i.e., duplicates must also be accounted for).

If no such substring exists, return an empty string (“”). You can assume that, if a valid substring exists, it will always be unique, meaning there won’t be multiple shortest substrings satisfying the condition.

Minimum Sliding Window

Explanation :“YXAZ” is the shortest substring that includes “X”, “Y”, and “Z” from string t.

Explanation: “apzo” is the smallest substring in which “oza” can be found.

Constraints:

  • 1 <= s1.length, s2.length <= 1000

Program for Smallest Substring in a string that contains all the characters of another string Solution

Recommendation for Time and Space Complexity – You should aim for a solution with O(n) time and O(m) space, where n is the length of the string s and m is the number of unique characters in s and t.

Hints for solving problems

Hint 1 :

A brute force solution would involve checking every substring of s against t and returning the minimum length valid substring. This would be an O(n^2) solution. Can you think of a better way? Maybe you should think in terms of frequency of characters.

Hint 2 :

We need to find substrings in s that should have atleast the characters of t. We can use hash maps to maintain the frequencies of characters. It will be O(1) for lookups. Can you think of an algorithm now?

There are mainly 2 approach to solve this problem-

  1. Brute Force Method
  2. Sliding Window Method

1. Brute Force Method

Generate all possible substrings of s and check if each contains all characters of t. Among valid substrings, return the shortest one. This approach is highly inefficient due to its exponential time complexity.

  • Time complexity: O(n^2)
  • Space complexity: O(m)

where n is the length of the string s and m is the total number of unique characters in the strings t and s.

Code

2. Sliding Window Method

Use two pointers to create a dynamic window in s, expanding and contracting it to include all characters of t. Maintain a frequency map to track the characters and efficiently find the smallest valid window.

  • Time complexity: O(n)
  • Space complexity: O(m)

where n is the length of the string s and m is the total number of unique characters in the strings t and s.

Code

More Articles

Minimum Window Substring Leetcode Solution

Minimum Window Substring Leetcode Problem :

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.

Minimum Window Substring Leetcode Problem Solution :

Constraints :

  • m == s.length
  • n == t.length
  • 1 <= m, n <= 105
  • s and t consist of uppercase and lowercase English letters.

Example 1:

  • Input: s = “a”, t = “aa”
  • Output: “”
  • Explanation: Both ‘a’s from t must be included in the window.
    Since the largest window of s only has one ‘a’, return empty string.

Example 2:

  • Input: s = “a”, t = “a”
  • Output: “a”
  • Explanation: The entire string s is the minimum window.

Idea to solve:

The problem asks to find the minimum window in s that contains all the characters of t. One way to approach this problem is to use a sliding window technique. We can maintain a window that starts from the beginning of s and moves forward until it contains all the characters of t. Once we have such a window, we can try to shrink it by moving the window’s start pointer forward while still keeping all the characters of t in the window. This will give us the minimum window.

Approach:

  1. Check if s is shorter than t. If it is, there is no possible solution, and we return an empty string.
  2. Create a frequency map of characters in t.
  3. Initialize count, start, min_length, and min_start to 0.
  4. Traverse the string s using the end pointer.
  5. If the current character in s is present in the frequency map, increment the count.
  6. Decrement the frequency of the current character in the frequency map.
  7. If the count equals the length of t, it means we have found a window that contains all characters of t. Now we try to shrink the window by moving the start pointer forward until the window still contains all the characters of t.
  8. If the length of the current window is smaller than the minimum length so far, update the minimum length and the minimum start.
  9. Increment the frequency of the character at the start pointer and decrement the count.
  10. Return the minimum window or an empty string if no window exists.

Complexity:

  • Time complexity: O(N), where N is the length of the string s.
    We traverse the string s once.

  • Space complexity: O(M), where M is the length of the string t.
    We create a frequency map of characters in t.

Minimum-Window-Substring-Solution

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :