forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
19 lines (19 loc) · 629 Bytes
/
s2.cpp
File metadata and controls
19 lines (19 loc) · 629 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// OJ: https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3332/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
int cnt[26] = {};
vector<int> ans;
int M = s.size(), N = p.size(), count = N;
for (char c : p) ++cnt[c - 'a'];
for (int i = 0; i < M; ++i) {
if (i >= N && cnt[s[i - N] - 'a']++ >= 0) ++count;
if (cnt[s[i] - 'a']-- > 0) --count;
if (!count) ans.push_back(i - N + 1);
}
return ans;
}
};