-
Notifications
You must be signed in to change notification settings - Fork 0
/
438-find-all-anagrams-in-a-string.cpp
55 lines (44 loc) · 1.74 KB
/
438-find-all-anagrams-in-a-string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Title: Find All Anagrams in a String
// Description:
// Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.
// An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
// Link: https://leetcode.com/problems/find-all-anagrams-in-a-string/
// Time complexity: O(m+n)
// Space complexity: O(m)
class Solution {
public:
std::vector<int> findAnagrams(std::string s, std::string p) {
assert(p.length() != 0);
std::vector<int> charCounts(26, 0);
std::size_t diffCount = 0;
// require every char in p to be matched
for (char c: p) {
int &charCount = charCounts[c - 'a'];
if (charCount == 0) ++diffCount;
charCount -= 1;
// if (charCount == 0) --diffCount;
}
std::vector<int> result;
for (std::size_t i = 0, j = 0; j != s.length();) {
// shrink the window if the substring is full
if (j-i == p.length()) {
char c = s[i++];
int &charCount = charCounts[c - 'a'];
if (charCount == 0) ++diffCount;
charCount -= 1;
if (charCount == 0) --diffCount;
}
// extend the window
if (true) {
char c = s[j++];
int &charCount = charCounts[c - 'a'];
if (charCount == 0) ++diffCount;
charCount += 1;
if (charCount == 0) --diffCount;
}
// an anagram is found
if (diffCount == 0) result.push_back(i);
}
return result;
}
};