-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
43 lines (43 loc) · 992 Bytes
/
Solution.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
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int n = s1.size(), m = s2.size();
if (n > m) {
return false;
}
vector<int> cnt(26);
for (int i = 0; i < n; ++i) {
--cnt[s1[i] - 'a'];
++cnt[s2[i] - 'a'];
}
int diff = 0;
for (int x : cnt) {
if (x) {
++diff;
}
}
if (diff == 0) {
return true;
}
for (int i = n; i < m; ++i) {
int a = s2[i - n] - 'a';
int b = s2[i] - 'a';
if (cnt[b] == 0) {
++diff;
}
if (++cnt[b] == 0) {
--diff;
}
if (cnt[a] == 0) {
++diff;
}
if (--cnt[a] == 0) {
--diff;
}
if (diff == 0) {
return true;
}
}
return false;
}
};