-
Notifications
You must be signed in to change notification settings - Fork 0
/
76. Minimum Window Substring 2.cpp
52 lines (41 loc) · 1.05 KB
/
76. Minimum Window Substring 2.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
// https://leetcode.com/problems/minimum-window-substring/
class Solution
{
public:
string minWindow(string s, string t)
{
if (t.length() > s.length())
return "";
string result = "";
vector<int> freq(128);
int start = -1;
int minWindowSize = INT_MAX;
int i = 0;
int j = 0;
int counter = 0;
for (char &c : t)
freq[c]++;
while (j < s.length())
{
if (--freq[s[j]] >= 0)
counter++;
while (counter == t.length())
{
int currentWindowSize = j - i + 1;
if (currentWindowSize < minWindowSize)
{
minWindowSize = currentWindowSize;
start = i;
}
if (++freq[s[i]] > 0)
counter--;
i++;
}
j++;
}
if (start == -1)
return "";
result = s.substr(start, minWindowSize);
return result;
}
};