-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0076.cpp
37 lines (30 loc) · 842 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
// Time O(M+N), Space O(M+N)
#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
string minWindow(string s, string t) {
int m = s.length(), n = t.length();
if (m < n) return "";
unordered_map<char, int> hm;
for (char c : t) hm[c]++;
int sPos = -1, minLen = INT_MAX;
for (int l = 0, r = 0, need = n; r < m; r++)
{
if (hm[s[r]] > 0) need--;
hm[s[r]]--;
while (need == 0)
{
if (r - l + 1 < minLen)
{
sPos = l;
minLen = r - l + 1;
}
if (hm[s[l]] >= 0) need++;
hm[s[l++]]++;
}
}
return sPos == -1 ? "" : s.substr(sPos, minLen);
}
};