BackmediumTwo PointersMicrosoft

Minimum Window Substring with Two Pointers Solution

Problem Statement

Given two strings s and t, write a function to find the minimum window in s that contains all characters of t. If no such window exists, return an empty string.

Example 1
Input
s = 'ADOBECODEBANC', t = 'ABC'
Output
BANC

Explanation: Step-by-step: 1. Initialize two pointers, one at the start and one at the end of string s. 2. Initialize a frequency dictionary to store the count of characters in string t. 3. Move the end pointer to the right and update the frequency dictionary. 4. If the frequency dictionary is complete (i.e., all characters of string t are present), calculate the window size and update the minimum window if necessary. 5. Move the start pointer to the right and repeat steps 3-5 until the end pointer reaches the end of string s.

Example 2
Input
s = 'a', t = 'aa'
Output
a

Explanation: Step-by-step: 1. Initialize two pointers, one at the start and one at the end of string s. 2. Initialize a frequency dictionary to store the count of characters in string t. 3. Move the end pointer to the right and update the frequency dictionary. 4. If the frequency dictionary is complete (i.e., all characters of string t are present), calculate the window size and update the minimum window if necessary. 5. Move the start pointer to the right and repeat steps 3-5 until the end pointer reaches the end of string s.

Constraints

  • 1 <= s.length <= 10^5
  • 1 <= t.length <= 10^4
  • s and t consist of lowercase English letters only
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Minimum Window Substring with Two Pointers — Problem Statement & Solution Guide

Two PointersMediumTwo Pointer Pattern
TimeO(n*m)
|
SpaceO(m)

Problem Description

Given two strings s and t, write a function to find the minimum window in s that contains all characters of t. If no such window exists, return an empty string.

Examples

Example 1

Input

s = 'ADOBECODEBANC', t = 'ABC'

Output

BANC

Explanation: Step-by-step: 1. Initialize two pointers, one at the start and one at the end of string s. 2. Initialize a frequency dictionary to store the count of characters in string t. 3. Move the end pointer to the right and update the frequency dictionary. 4. If the frequency dictionary is complete (i.e., all characters of string t are present), calculate the window size and update the minimum window if necessary. 5. Move the start pointer to the right and repeat steps 3-5 until the end pointer reaches the end of string s.

Example 2

Input

s = 'a', t = 'aa'

Output

a

Explanation: Step-by-step: 1. Initialize two pointers, one at the start and one at the end of string s. 2. Initialize a frequency dictionary to store the count of characters in string t. 3. Move the end pointer to the right and update the frequency dictionary. 4. If the frequency dictionary is complete (i.e., all characters of string t are present), calculate the window size and update the minimum window if necessary. 5. Move the start pointer to the right and repeat steps 3-5 until the end pointer reaches the end of string s.

Constraints

  • 1 <= s.length <= 10^5
  • 1 <= t.length <= 10^4
  • s and t consist of lowercase English letters only

Optimal Approach & Strategy

The optimized approach uses the two-pointer technique and a hashmap to count the frequency of characters, resulting in a time complexity of O(n).

Brute Force Approach

The brute force approach involves checking every possible substring of s to see if it contains all characters of t, resulting in a time complexity of O(n^2).

Asked in Top Tech Interviews

Microsoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.