knakul853

Untitled

Jun 21st, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4.  
  5. class Solution {
  6. public:
  7.     string minWindow(string s, string t) {
  8.        
  9.         int n = (int)s.size();
  10.         int rem = (int)t.size();
  11.         vector<int>dict(256,0);
  12.        
  13.         for(char c:t)dict[c]++; // mark the all pattern in dictionary
  14.        
  15.         int ans = INT_MAX;
  16.         int start = INT_MAX;
  17.         int l=0;
  18.         int i=0;
  19.         while( i < n )
  20.         {
  21.             dict[s[i]]--; // counter the presence of the current character.
  22.            
  23.             if(dict[s[i++]]>=0) rem--; // if current character mached dec..it's frequencies
  24.            
  25.            
  26.             while(rem == 0) // we got the match exctract it's length.
  27.             {
  28.                 if( ans > i-l)
  29.                 {
  30.                     ans = i-l;
  31.                     start = l;
  32.                 }
  33.                
  34.                 dict[s[l]]++;
  35.                 if(dict[s[l++]] > 0) rem++;  // try to decrease it's lenth as much as possible.
  36.             }
  37.         }
  38.        
  39.        
  40.         return ans==INT_MAX ? "":s.substr(start, ans);
  41.        
  42.     }
  43. };
Add Comment
Please, Sign In to add comment