Minimum Window Substring

Sliding Window / 2 Pointer Longest and Smallest Window Problems Hard

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