Minimum Window Substring
Updated
Problem statement
You are given two strings s and t. Find the shortest substring of s (a run of consecutive characters) that contains every character of t, including repeats: if a character appears k times in t, the substring must contain it at least k times. Upper and lower case letters are different characters.
Return that substring. If several substrings tie for the shortest length, return the one that starts earliest in s. If no substring of s works, return the empty string "".
Function
minWindow(s: String, t: String) → StringExamples
Example 1
s = "XQAZBYAB"t = "AB"return = "AB""AZB" (start 2) holds both letters, but "AB" at the end is shorter. No window shorter than 2 can hold two characters.
Example 2
s = "cabbcaab"t = "abb"return = "abb"t needs one a and two b. The window "abb" starting at index 1 has exactly that, and no window of length 3 starts earlier with enough letters.
Example 3
s = "aBcAb"t = "ab"return = "aBcAb"Upper and lower case are different characters. The only lowercase a is at index 0 and the only lowercase b is at index 4, so the window has to cover the whole string.
Example 4
s = "pqqp"t = "qp"return = "pq"Both "pq" (start 0) and "qp" (start 2) have length 2 and hold one p and one q. The one that starts earlier wins.
Example 5
s = "abc"t = "cc"return = ""t needs two copies of c, but s has only one. No window works, so the answer is the empty string.
Constraints
1 <= s.length, t.length <= 105sandtcontain only uppercase and lowercase English letters.
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Minimum Window SubstringLyft · PHONE SCREENLyft interview questions
- Minimum Window SubstringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Window SubstringSalesforce · PHONE SCREENSalesforce interview questions
- Minimum Window SubstringCharacter.AI · PHONE SCREEN
- Shortest Window Covering All LettersAdobe · PHONE SCREENAdobe interview questions
- Minimum Window SubstringSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Longest Substring Without Repeating CharactersAngel One · PHONE SCREEN
- Longest Substring Without Repeating CharactersNykaa · PHONE SCREENNykaa interview questions
- Longest Substring Without Repeating CharactersIBM · PHONE SCREENIBM interview questions
- Longest Substring Without Repeating CharactersTikTok · PHONE SCREENTikTok interview questions