File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
0076-minimum-window-substring Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments