Rohit4Pal

Untitled

Aug 16th, 2021
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string minWindow(string s, string t) {
  4.  
  5.         int count=0;
  6.         map<char,int> mp;
  7.         for(char c:t){
  8.             mp[c]++;
  9.         }
  10.  
  11.         count=mp.size();
  12.         int minWindow=INT_MAX,start=-1,end=-1;
  13.         for(int i=0,j=0;i<s.length();){
  14.  
  15.             if(count>0){
  16.                 if(mp.find(s[j]) != mp.end()){
  17.                     mp[s[j]]--;
  18.                 }
  19.                 if(mp[s[j]]==0 )    count--;
  20.                 j++;
  21.             }else{
  22.                 if(mp.find(s[i]) != mp.end()){
  23.  
  24.                     if(minWindow>j-i){
  25.                         minWindow=j-i;
  26.                         start=i,end=j-1;
  27.                     }
  28.                     if(mp[s[i]]<0)  //there are more of this character
  29.                         mp[s[i]]++;
  30.                     if(mp[s[i]]>0)  count++;
  31.                     i++;
  32.                 }
  33.             }
  34.         }
  35.  
  36.         string res;
  37.         while(start<=end){
  38.             res+=s[start++];
  39.         }
  40.         return res;
  41.     }
  42.  
  43. };
Advertisement
Add Comment
Please, Sign In to add comment