码迷,mamicode.com
首页 > 其他好文 > 详细

[滑动窗口/哈希] leetcode 567 Permutation in String

时间:2019-07-31 22:24:45      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:target   false   color   problems   perm   滑动   als   tor   logs   

problem:https://leetcode.com/problems/permutation-in-string/

        这道题感觉几乎和Leetcode上另一题一模一样,昨天刚刷的:https://www.cnblogs.com/fish1996/p/11269526.html,就当签到题爽一爽了。

class Solution {
public:
    bool checkInclusion(string s1, string s2) {

        vector<int> target(26, 0);
        for (int i = 0; i < s1.size(); i++)
        {
            target[s1[i] - a]++;
        }

        int k = s1.size();

        vector<int> source(26, 0);
        int count = 0;
        for (int i = 0; i < s2.size(); i++)
        {
            source[s2[i] - a]++;
            if (source[s2[i] - a] <= target[s2[i] - a])
            {
                count++;
            }

            if (i >= k)
            {        
                if (source[s2[i - k] - a] <= target[s2[i - k] - a])
                {
                    count--;
                }
                source[s2[i - k] - a]--;
            }

            if (count == k)
            {
                return true;
            }
        }

        return false;
    }
};

 

[滑动窗口/哈希] leetcode 567 Permutation in String

标签:target   false   color   problems   perm   滑动   als   tor   logs   

原文地址:https://www.cnblogs.com/fish1996/p/11279050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!