Skip to content

Commit 1c8824d

Browse files
committed
Sync LeetCode submission Runtime - 65 ms (75.61%), Memory - 18.2 MB (39.66%)
1 parent 8ca9dd3 commit 1c8824d

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>t</code><em> (<strong>including duplicates</strong>) is included in the window</em>. If there is no such substring, return <em>the empty string </em><code>&quot;&quot;</code>.</p>
2+
3+
<p>The testcases will be generated such that the answer is <strong>unique</strong>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> s = &quot;ADOBECODEBANC&quot;, t = &quot;ABC&quot;
10+
<strong>Output:</strong> &quot;BANC&quot;
11+
<strong>Explanation:</strong> The minimum window substring &quot;BANC&quot; includes &#39;A&#39;, &#39;B&#39;, and &#39;C&#39; from string t.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s = &quot;a&quot;, t = &quot;a&quot;
18+
<strong>Output:</strong> &quot;a&quot;
19+
<strong>Explanation:</strong> The entire string s is the minimum window.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s = &quot;a&quot;, t = &quot;aa&quot;
26+
<strong>Output:</strong> &quot;&quot;
27+
<strong>Explanation:</strong> Both &#39;a&#39;s from t must be included in the window.
28+
Since the largest window of s only has one &#39;a&#39;, return empty string.
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>m == s.length</code></li>
36+
<li><code>n == t.length</code></li>
37+
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
38+
<li><code>s</code> and <code>t</code> consist of uppercase and lowercase English letters.</li>
39+
</ul>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Follow up:</strong> Could you find an algorithm that runs in <code>O(m + n)</code> time?</p>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Approach: Sliding Window
2+
3+
# n = len(s), m = len(t)
4+
# Time: O(m + n)
5+
# Space: O(m + n)
6+
7+
from collections import Counter
8+
9+
class Solution:
10+
def minWindow(self, s: str, t: str) -> str:
11+
if not t or not s:
12+
return ''
13+
14+
t_count = Counter(t)
15+
required = len(t_count) # No. of unique chars in t
16+
17+
# formed is used to keep track of how many unique characters in t are present in the current window in its desired frequency.
18+
formed = 0
19+
window_counts = {}
20+
21+
left, right = 0, 0
22+
min_length = float('inf')
23+
min_start = 0
24+
25+
while right < len(s):
26+
char = s[right]
27+
window_counts[char] = window_counts.get(char, 0) + 1
28+
29+
if char in t_count and window_counts[char] == t_count[char]:
30+
formed += 1
31+
32+
# If all required characters are found in the window
33+
while left <= right and formed == required:
34+
char = s[left]
35+
36+
# If the current window size is smaller than the min window size
37+
if min_length > right - left + 1:
38+
min_length = right - left + 1
39+
min_start = left
40+
41+
window_counts[char] -= 1
42+
43+
if char in t_count and window_counts[char] < t_count[char]:
44+
# If the character is in t and its count in the window is less than its count in t
45+
formed -= 1
46+
left += 1 # shrink window
47+
48+
right += 1 # expand window
49+
50+
if min_length > len(s):
51+
return ''
52+
53+
return s[min_start : min_start + min_length]
54+

0 commit comments

Comments
 (0)