-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum-window-substr.cpp
More file actions
39 lines (37 loc) · 902 Bytes
/
minimum-window-substr.cpp
File metadata and controls
39 lines (37 loc) · 902 Bytes
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
using namespace std;
#include <iostream>
#include <bits/stdc++.h>
//https://leetcode.com/problems/minimum-window-substring/
class Solution {
public:
string minWindow(string s, string t) {
map<char,int> mp;
for(int i=0;i<t.length();i++){
mp[t[i]]++;
}
int l = 0;
int r = 0;
int cnt=0;
int len = 1000000000;
int indx = -1;
while( r < s.length()){
if(mp[s[r]]>0){
cnt+=1;
}
mp[s[r]]--;
while(cnt==t.length()){
if(r-l+1<len){
len = r-l+1;
indx=l;
}
mp[s[l]]++;
if(mp[s[l]]>0){
cnt-=1;
}
l++;
}
r++;
}
return indx==-1 ? "" : s.substr(indx,len);
}
};