forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0076.cpp
38 lines (37 loc) · 971 Bytes
/
0076.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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
string minWindow(string s, string t)
{
vector<int> mem(128, 0);
for (auto& c : t) mem[c]++;
unsigned int t_len = t.size();
int minL = 0, minR = INT_MAX, l = 0;
for (unsigned int r = 0; r < s.size(); ++r)
{
if (mem[s[r]]-- > 0) --t_len;
if (t_len == 0)
{
while (mem[s[l]] < 0) ++mem[s[l++]];
if (r - l < minR - minL)
{
minL = l; minR = r;
}
++mem[s[l++]];
++t_len;
}
}
return minR == INT_MAX ? "" : s.substr(minL, minR-minL+1);
}
};
int main()
{
string s = "ADOBECODEBANC", t = "ABC";
cout << Solution().minWindow(s, t);
return 0;
}