Minimum Window Substring

Sliding Window / 2 Pointer Longest and Smallest Window Problems Hard
  • This problem, often referred to as the Minimum Window Substring problem, is frequently used in text processing applications to find the shortest relevant context for a set of keywords
  • For instance, in search engines, this concept can be adapted to extract the smallest snippet of text that contains all the search terms inputted by a user
  • Similarly, in Natural Language Processing (NLP), variations of this problem are used for tasks like entity recognition and sentiment analysis where relevant information needs to be extracted from larger blocks of text

Given two strings s and t. Find the smallest window substring of s that includes all characters in t (including duplicates) , in the window. Return the empty string "" if no such substring exists.

Examples:

Input : s = "ADOBECODEBANC" , t = "ABC"

Output : "BANC"

Explanation : The minimum window substring of string s that contains the string t is "BANC".

Input : s = "a" , t = "a"

Output : "a"

Explanation : The complete string is the minimum window

Input : s = "aAbBDdcC" , t = "Bc"

Constraints

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

Hints

  • Use a sliding window with two pointers (left and right) to find the smallest substring in s that contains all characters in t.
  • Create a frequency map for characters in t. Use another map to track the characters in the current window of s.

Company Tags

Shopify Lyft Texas Instruments IBM Unity Technologies JPMorgan Chase Salesforce Cloudflare Swiggy ARM MongoDB Boston Consulting Group Morgan Stanley American Express KPMG Bungie Epic Systems Philips Healthcare Instacart PayPal Reddit Bain & Company Medtronic Splunk Rakuten Google Microsoft Amazon Meta Apple Netflix Adobe