Skip to content

Commit 3a18c12

Browse files
committed
3
1 parent d9b50ad commit 3a18c12

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
string minWindow(string S, string T) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
int needFind[256] = {0};
8+
int hasFound[256] = {0};
9+
int count = 0, minLen = INT_MAX;
10+
string result = "";
11+
12+
for (int i = 0; i < T.size(); i++)
13+
needFind[T[i]] += 1;
14+
15+
for (int low = 0, high = 0; high < S.size(); high++) {
16+
if (needFind[S[high]] == 0)
17+
continue;
18+
19+
hasFound[S[high]] += 1;
20+
if (hasFound[S[high]] <= needFind[S[high]])
21+
count += 1;
22+
23+
if (count == T.size()) {
24+
while (low < high) {
25+
if (needFind[S[low]] == 0) {
26+
low += 1;
27+
continue;
28+
}
29+
if (hasFound[S[low]] > needFind[S[low]])
30+
hasFound[S[low]] -= 1;
31+
else
32+
break;
33+
low += 1;
34+
}
35+
if (minLen > high - low + 1) {
36+
minLen = high - low + 1;
37+
result = S.substr(low, minLen);
38+
}
39+
}
40+
}
41+
return result;
42+
}
43+
};

0 commit comments

Comments
 (0)