-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0438_findAnagrams.cc
103 lines (97 loc) · 1.96 KB
/
Problem_0438_findAnagrams.cc
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
vector<int> findAnagrams(string s, string p)
{
vector<int> ans;
if (s.length() == 0 || p.length() == 0 || s.length() < p.length())
{
return ans;
}
int N = s.length();
int M = p.length();
unordered_map<char, int> map;
// 先统计p字符串的字符
for (auto& c : p)
{
if (!map.count(c))
{
map.emplace(c, 1);
}
else
{
map[c]++;
}
}
int all = M;
// 滑动窗口,如果窗口长度不够,统计消耗的字符数
for (int end = 0; end < M - 1; end++)
{
if (map.count(s[end]))
{
int count = map.at(s[end]);
if (count > 0)
{
all--;
}
map[s[end]]--;
}
}
// 窗口长度够,就同时往右移动
for (int end = M - 1, start = 0; end < N; end++, start++)
{
if (map.count(s[end]))
{
// 右边字符进入
int count = map.at(s[end]);
if (count > 0)
{
all--;
}
map[s[end]]--;
}
if (all == 0)
{
// 刚好抵消,说明找到了一种答案
ans.push_back(start);
}
if (map.count(s[start]))
{
// 左边字符出去
int count = map.at(s[start]);
if (count >= 0)
{
all++;
}
map[s[start]]++;
}
}
return ans;
}
};
bool isVectorEuqal(vector<int> a, vector<int> b)
{
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
return a == b;
}
void testFindAnagrams()
{
Solution s;
vector<int> o1 = {0, 6};
vector<int> o2 = {0, 1, 2};
EXPECT_TRUE(isVectorEuqal(o1, s.findAnagrams("cbaebabacd", "abc")));
EXPECT_TRUE(isVectorEuqal(o2, s.findAnagrams("abab", "ab")));
EXPECT_SUMMARY;
}
int main()
{
testFindAnagrams();
return 0;
}