-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum Window Substring.txt
49 lines (49 loc) · 1.57 KB
/
Minimum Window Substring.txt
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
class Solution {
public:
string minWindow(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> tuse(255, 0), suse(255, 0);
vector<char> tcha;
string ret;
int tsize = T.size(), ssize = S.size(), snum = 0, l = -1, i = 0, w = INT_MAX;
if (!tsize || !ssize || (tsize > ssize))
return "";
for (int i = 0; i < tsize; i++) {
tuse[T[i]]++;
if (tuse[T[i]] == 1)
tcha.push_back(T[i]);
}
while (i < ssize) {
if (tuse[S[i]] != 0) {
l = (l == -1)?i:l;
suse[S[i]]++;
snum++;
if (tuse[S[i]] < suse[S[i]]) {
while(true) {
if (tuse[S[l]] == 0)
l++;
else if (tuse[S[l]] < suse[S[l]]) {
suse[S[l]]--;
snum--;
l++;
} else
break;
}
}
if ((snum >= tsize) && check(tuse, suse, tcha) && (w > (i - l + 1))) {
w = i - l + 1;
ret = S.substr(l, w);
}
}
i++;
}
return ret;
}
bool check(vector<int>& t, vector<int>& s, vector<char>& c) {
for (size_t i = 0; i < c.size(); i++)
if (t[c[i]] > s[c[i]])
return false;
return true;
}
};