首页 > 其他 > 详细

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

时间:2019-07-31 22:03:30      阅读:93      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!