Skip to content

Commit 3d16449

Browse files
Time: 24 ms (72.68%), Space: 16.4 MB (15.22%) - LeetHub
1 parent 6caaacd commit 3d16449

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution
2+
{
3+
public:
4+
string minWindow(string s, string t)
5+
{
6+
unordered_map<char, int> mp;
7+
for (int i = 0; i < t.size(); i++)
8+
{
9+
mp[t[i]]++;
10+
}
11+
int count = mp.size();
12+
string ans;
13+
14+
int mini = INT_MAX;
15+
16+
int i = 0;
17+
int j = 0;
18+
19+
while (j < s.size())
20+
{
21+
22+
if (mp.find(s[j]) != mp.end())
23+
{
24+
mp[s[j]]--;
25+
if (mp[s[j]] == 0)
26+
{
27+
count--;
28+
}
29+
}
30+
31+
while (count == 0)
32+
{
33+
if (mini > j - i + 1)
34+
{
35+
mini = min(mini, j - i + 1);
36+
ans = s.substr(i, j - i + 1);
37+
}
38+
if (mp.find(s[i]) != mp.end())
39+
{
40+
mp[s[i]]++;
41+
if (mp[s[i]] == 1)
42+
{
43+
count++;
44+
}
45+
}
46+
i++;
47+
}
48+
j++;
49+
}
50+
return ans;
51+
}
52+
};

0 commit comments

Comments
 (0)