Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string minWindow(string s, string t) {
- int count=0;
- map<char,int> mp;
- for(char c:t){
- mp[c]++;
- }
- count=mp.size();
- int minWindow=INT_MAX,start=-1,end=-1;
- for(int i=0,j=0;i<s.length();){
- if(count>0){
- if(mp.find(s[j]) != mp.end()){
- mp[s[j]]--;
- }
- if(mp[s[j]]==0 ) count--;
- j++;
- }else{
- if(mp.find(s[i]) != mp.end()){
- if(minWindow>j-i){
- minWindow=j-i;
- start=i,end=j-1;
- }
- if(mp[s[i]]<0) //there are more of this character
- mp[s[i]]++;
- if(mp[s[i]]>0) count++;
- i++;
- }
- }
- }
- string res;
- while(start<=end){
- res+=s[start++];
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment