Advertisement
nikunjsoni

76

Apr 18th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string minWindow(string s, string t) {
  4.         unordered_map<char, int> count;
  5.         for(char c: t) count[c]++;
  6.         int counter=t.size(), left, right;
  7.        
  8.         int ans=INT_MAX, idx=0;
  9.         for(left=0, right=0; right<s.size(); right++){
  10.             if(count[s[right]]-- > 0) counter--;
  11.             while(counter == 0){
  12.                 if(right-left+1 < ans){
  13.                     ans = min(ans, right-left+1);
  14.                     idx = left;
  15.                 }
  16.                 if(count[s[left++]]++ == 0) counter++;
  17.             }
  18.         }
  19.         return ans==INT_MAX ? "" : s.substr(idx,ans);
  20.     }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement