forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
22 lines (22 loc) · 674 Bytes
/
s1.cpp
File metadata and controls
22 lines (22 loc) · 674 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// OJ: https://leetcode.com/problems/find-all-anagrams-in-a-string/
// Author: github.com/lzl124631x
// Time: O(M + N)
// Space: O(1)
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
int target[26] = {}, cnt[26] = {};
for (char c : p) target[c - 'a']++;
vector<int> ans;
for (int i = 0; i < s.size(); ++i) {
if (i >= p.size()) cnt[s[i - p.size()] - 'a']--;
cnt[s[i] - 'a']++;
int j = 0;
for (; j < 26; ++j) {
if (cnt[j] != target[j]) break;
}
if (j == 26) ans.push_back(i - p.size() + 1);
}
return ans;
}
};