567. Permutation in String

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

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string.

Example 1:

Input:s1 = "ab" s2 = "eidbaooo"

Output:True

Explanation: s2 contains one permutation of s1 ("ba").

Example 2:

Input:s1= "ab" s2 = "eidboaoo"

Output: False

Thoughts

s2是否存在s1的permutation。permutation => 静态窗口。用freq map统计s1的元素出现次数,然后维持一个s1 size的窗口扫描s2,当遇到元素与map中的元素相同时,把相应的freq++, 同时把窗口最前面元素剔除并把(如果有的话)相应的--,并用cnt记录freq为0的个数。 当cnt == len(s1),表明这一段刚好有且仅有s1中所有元素。

Code

/*
 * @lc app=leetcode id=567 lang=cpp
 *
 * [567] Permutation in String
 */

// @lc code=start
class Solution {
public:
    bool checkInclusion(string s1, string s2) {
       const int M = s1.length(), N = s2.length();
       if (M > N) return false;
       vector<int> freq(26, 0);
       for (const auto c : s1) --freq[c - 'a']; 
       int cnt = 0;
       for (int i = 0; i < M; ++i) {
           const auto k = s2[i] - 'a';
           ++freq[k];
           if (freq[k] <= 0) ++cnt;
       }
       if (cnt == M) return true;
       for (int l = 0, r = M; r < N; ++r, ++l) {
           ++freq[s2[r] - 'a'];
           if (freq[s2[r] - 'a'] <= 0) ++cnt;
           if (freq[s2[l] - 'a'] <= 0) --cnt;
           --freq[s2[l] - 'a'];
           if (cnt == M) return true;
       }
       return false;
    }
};
// @lc code=end

Analysis

时间复杂度O(n), 空间O(1).

Last updated

Was this helpful?