Leetcode: sliding window+two pointers

这种题有个规律,
1,移动end pointer
2,找到目标(if count == 0)
3,移动begin pointer

前面两题目标string是固定长度,后一题目标string是找到最短的,不同的处理在第三步。每次移动begin pointer要保证把map和count复原,这样才有机会找到下一个candidate。

438 and 439 are exactly the same.
438. Find All Anagrams in a String
https://leetcode.com/problems/find-all-anagrams-in-a-string/description/

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int>map(256, 0);
        vector<int>res;     
        int begin = 0;
        int end = 0;       
        int count = p.size();
        
        for(auto c : p)
        {
            map[c] ++;
        }   
        while(end < s.size())
        {
            if(map[s[end]] > 0)
            {
                count --;        
            }
            map[s[end]]--;
            end ++;
            
            if(count == 0)
            {
                res.push_back(begin);             
            }
            
            if(end - begin == p.size())
            {
                if(map[s[begin]] >= 0)
                {
                    count ++;
                }
                map[s[begin]] ++;
                begin ++;
            }
        }
        return res;
    }
};
  1. Permutation in String
    https://leetcode.com/problems/permutation-in-string/description/
class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        vector<int>map(256, 0);

        int begin = 0;
        int end = 0;
        int count = s1.size();
        for(auto c : s1)
        {
            map[c]++;
        }
        while(end < s2.size())
        {
            if(map[s2[end]] > 0)
            {
                count--;
            }
            map[s2[end]]--;
            end++;
            if(count == 0)
            {
                return true;
            }
            if(end - begin == s1.size())
            {
                if(map[s2[begin]] >= 0)
                {
                    count++;
                }
                map[s2[begin]]++;
                begin++;
            }
        }
        return false;
    }
};
  1. Minimum Window Substring
    https://leetcode.com/problems/minimum-window-substring/description/
class Solution {
public:
    string minWindow(string s, string t) {
        vector<int>hash(256, 0);
        int end = 0, start = 0; //two pointer
        int count = t.size();
        int minlen = INT_MAX, minstart = 0;
        for(auto c:t)
        {
            hash[c]++;  //hash table saves the target string "char, times char apears"
        }
        while(end < s.size())
        {
            if(hash[s[end]] > 0) //if char found in s
            {
                count --;
            }
            hash[s[end]]--; //the unexpect char will be -1 or less
            end++;          //keeps move end
            while(count == 0) //all t found, when condition satisfied, 1, save the cur best;2, start++ and try to shrink win.
            {
                if((end - start) < minlen) //save the cur minwindow
                {
                    minlen = end - start;
                    minstart = start;
                }
                if(hash[s[start]] == 0) //try to break the condition. the start position char is expected and cannot shrink anymore, breakn the while loop and go back to move end. if not, count still == 0, and move start, the window shrinked (>= 0 also works)
                {
                    count ++;
                }
                hash[s[start]] ++; //restore init status and move start++
                start ++;
            }
        }
        
        return minlen == INT_MAX ? "" : s.substr(minstart, minlen);
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43476349/article/details/83748075