Minimum Swaps to Group All 1's Together
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.
Example 1:
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
Example 2:
Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.
Example 3:
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.
Constraints:
m == s.length
n == t.length
1 <= m, n <= 105
s
andt
consist of uppercase and lowercase English letters.
Follow up: Could you find an algorithm that runs in O(m + n)
time?
Solution
We will use a sliding window to find the minimum window.
We wish to use a charmap
to store the number of characters seen in s
, and we will use two indices to represent an interval (left inclusive, right exclusive) of the window
.
To ensure that all characters in t
is included, we can initialize charmap
to negative counts for each character in t
, so that when we iterate s
and increase the counter for each c
, we know that charmap[c] >= 0
means that s
contains enough c
to satisfy the condition.
So the window is valid when every value in charmap
is non-negative, thus we can find the smallest window.
Notice that our window interval is initialized with (-m, 0)
which has a length m
but will produce ""
if we were to obtain s[-m:0]
.
Implementation
def minWindow(self, s: str, t: str) -> str:
m, n = len(s), len(t)
if m < n:
return ""
charmap = defaultdict(int)
for c in t:
charmap[c] -= 1
window, l = (-m, 0), 0
for r in range(m):
charmap[s[r]] += 1
r += 1
while min(charmap.values()) >= 0: # valid
if window[1] - window[0] >= r - l: # smaller than current
window = (l, r)
charmap[s[l]] -= 1
l += 1
return s[window[0] : window[1]]
Ready to land your dream job?
Unlock your dream job with a 2-minute evaluator for a personalized learning plan!
Start EvaluatorWhich of the tree traversal order can be used to obtain elements in a binary search tree in sorted order?
Recommended Readings
LeetCode Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way we
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!