Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- knakul853
- **/
- class Solution {
- public:
- string minWindow(string s, string t) {
- int n = (int)s.size();
- int rem = (int)t.size();
- vector<int>dict(256,0);
- for(char c:t)dict[c]++; // mark the all pattern in dictionary
- int ans = INT_MAX;
- int start = INT_MAX;
- int l=0;
- int i=0;
- while( i < n )
- {
- dict[s[i]]--; // counter the presence of the current character.
- if(dict[s[i++]]>=0) rem--; // if current character mached dec..it's frequencies
- while(rem == 0) // we got the match exctract it's length.
- {
- if( ans > i-l)
- {
- ans = i-l;
- start = l;
- }
- dict[s[l]]++;
- if(dict[s[l++]] > 0) rem++; // try to decrease it's lenth as much as possible.
- }
- }
- return ans==INT_MAX ? "":s.substr(start, ans);
- }
- };
Add Comment
Please, Sign In to add comment