-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.cpp
More file actions
47 lines (40 loc) · 1.12 KB
/
MinimumWindowSubstring.cpp
File metadata and controls
47 lines (40 loc) · 1.12 KB
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
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char, int> mpSrc;
unordered_map<char, int> mpCur;
int nFindLen = 0;
queue<int> qIndex;
for (auto ch : t)
{
mpSrc[ch]++;
}
int nWinLeft = -1;
int nWinRight = s.size();
for (int i = 0; i < s.size(); i++)
{
if (mpSrc.count(s[i]) == 0)
continue;
mpCur[s[i]]++;
qIndex.push(i);
if (mpCur[s[i]] <= mpSrc[s[i]]) nFindLen++;
if (nFindLen == t.size())
{
int k = 0;
do
{
k = qIndex.front();
qIndex.pop();
mpCur[s[k]]--;
} while (mpCur[s[k]] >= mpSrc[s[k]]);
if (i - k < nWinRight - nWinLeft)
{
nWinRight = i;
nWinLeft = k;
}
nFindLen--;
}
}
return nWinLeft == -1 ? "" : s.substr(nWinLeft, nWinRight - nWinLeft + 1);
}
};