funccheckInclusion(s1, s2 string)bool { n, m := len(s1), len(s2) if n > m { returnfalse } var cnt1, cnt2 [26]int for i, ch := range s1 { cnt1[ch-'a']++ cnt2[s2[i]-'a']++ } if cnt1 == cnt2 { returntrue } for i := n; i < m; i++ { cnt2[s2[i]-'a']++ cnt2[s2[i-n]-'a']-- if cnt1 == cnt2 { returntrue } } returnfalse }
funccheckInclusion(s1, s2 string)bool { n, m := len(s1), len(s2) if n > m { returnfalse } cnt := [26]int{} for i, ch := range s1 { cnt[ch-'a']-- cnt[s2[i]-'a']++ } diff := 0 for _, c := range cnt[:] { if c != 0 { diff++ } } if diff == 0 { returntrue } for i := n; i < m; i++ { x, y := s2[i]-'a', s2[i-n]-'a' if x == y { continue } if cnt[x] == 0 { diff++ } cnt[x]++ if cnt[x] == 0 { diff-- } if cnt[y] == 0 { diff++ } cnt[y]-- if cnt[y] == 0 { diff-- } if diff == 0 { returntrue } } returnfalse }
boolcheckInclusion(char* s1, char* s2) { int n = strlen(s1), m = strlen(s2); if (n > m) { returnfalse; } int cnt[26]; memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; ++i) { --cnt[s1[i] - 'a']; ++cnt[s2[i] - 'a']; } int diff = 0; for (int i = 0; i < 26; ++i) { if (cnt[i] != 0) { ++diff; } } if (diff == 0) { returntrue; } for (int i = n; i < m; ++i) { int x = s2[i] - 'a', y = s2[i - n] - 'a'; if (x == y) { continue; } if (cnt[x] == 0) { ++diff; } ++cnt[x]; if (cnt[x] == 0) { --diff; } if (cnt[y] == 0) { ++diff; } --cnt[y]; if (cnt[y] == 0) { --diff; } if (diff == 0) { returntrue; } } returnfalse; }
classSolution { public: boolcheckInclusion(string s1, string s2){ int n = s1.length(), m = s2.length(); if (n > m) { returnfalse; } vector<int> cnt(26); for (int i = 0; i < n; ++i) { --cnt[s1[i] - 'a']; } int left = 0; for (int right = 0; right < m; ++right) { int x = s2[right] - 'a'; ++cnt[x]; while (cnt[x] > 0) { --cnt[s2[left] - 'a']; ++left; } if (right - left + 1 == n) { returntrue; } } returnfalse; } };
funccheckInclusion(s1, s2 string)bool { n, m := len(s1), len(s2) if n > m { returnfalse } cnt := [26]int{} for _, ch := range s1 { cnt[ch-'a']-- } left := 0 for right, ch := range s2 { x := ch - 'a' cnt[x]++ for cnt[x] > 0 { cnt[s2[left]-'a']-- left++ } if right-left+1 == n { returntrue } } returnfalse }
boolcheckInclusion(char* s1, char* s2) { int n = strlen(s1), m = strlen(s2); if (n > m) { returnfalse; } int cnt[26]; memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; ++i) { --cnt[s1[i] - 'a']; } int left = 0; for (int right = 0; right < m; ++right) { int x = s2[right] - 'a'; ++cnt[x]; while (cnt[x] > 0) { --cnt[s2[left] - 'a']; ++left; } if (right - left + 1 == n) { returntrue; } } returnfalse; }
var checkInclusion = function(s1, s2) { const n = s1.length, m = s2.length; if (n > m) { returnfalse; } const cnt = newArray(26).fill(0); for (let i = 0; i < n; ++i) { --cnt[s1[i].charCodeAt() - 'a'.charCodeAt()]; } let left = 0; for (let right = 0; right < m; ++right) { const x = s2[right].charCodeAt() - 'a'.charCodeAt(); ++cnt[x]; while (cnt[x] > 0) { --cnt[s2[left].charCodeAt() - 'a'.charCodeAt()]; ++left; } if (right - left + 1 === n) { returntrue; } } returnfalse; };
复杂度分析
时间复杂度:O(n+m+|\Sigma|)。 创建 cnt 需要 O(|\Sigma|) 的时间。 遍历 s_1 需要 O(n) 的时间。 双指针遍历 s_2 时,由于 left 和 right 都只会向右移动,故这一部分需要 O(m) 的时间。