We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 05a6e0c commit 461ba12Copy full SHA for 461ba12
1 file changed
minimum-window-substring/PDKhan.cpp
@@ -0,0 +1,44 @@
1
+class Solution {
2
+ public:
3
+ string minWindow(string s, string t) {
4
+ unordered_map<char, int> t_map;
5
+ unordered_map<char, int> window;
6
+ int need = 0, match = 0;
7
+ int min_len = INT_MAX;
8
+ int min_low = 0;
9
+ int l = 0;
10
+
11
+ for(char ch : t){
12
+ if(t_map[ch] == 0)
13
+ need++;
14
15
+ t_map[ch]++;
16
+ }
17
18
+ for(int h = 0; h < s.length(); h++){
19
+ window[s[h]]++;
20
21
+ if(window[s[h]] == t_map[s[h]])
22
+ match++;
23
24
+ while(need == match){
25
+ if(h - l + 1 < min_len){
26
+ min_len = h - l + 1;
27
+ min_low = l;
28
29
30
+ window[s[l]]--;
31
32
+ if(window[s[l]] < t_map[s[l]])
33
+ match--;
34
35
+ l++;
36
37
38
39
+ if(min_len == INT_MAX)
40
+ return "";
41
42
+ return s.substr(min_low, min_len);
43
44
+ };
0 commit comments