-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path076-MinimumWindowSubstring.cs
40 lines (36 loc) · 1.33 KB
/
076-MinimumWindowSubstring.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//-----------------------------------------------------------------------------
// Runtime: 84ms
// Memory Usage: 25.6 MB
// Link: https://leetcode.com/submissions/detail/378572790/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _076_MinimumWindowSubstring
{
public string MinWindow(string s, string t)
{
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t) || t.Length > s.Length) { return string.Empty; }
var counts = new int[256];
foreach (var ch in t)
counts[ch]++;
var remaining = t.Length;
int left = 0, right = 0, minStart = 0, minLength = int.MaxValue;
while (right < s.Length)
{
if (--counts[s[right++]] >= 0) remaining--;
while (remaining == 0)
{
if (right - left < minLength)
{
minLength = right - left;
minStart = left;
}
if (++counts[s[left++]] > 0) remaining++;
}
if (minLength == t.Length)
break;
}
return minLength == int.MaxValue ? string.Empty : s.Substring(minStart, minLength);
}
}
}