-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path567.cpp
56 lines (56 loc) · 1.13 KB
/
567.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
56
// conciseidea.cpp
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int count[26] = {};
int n1 = s1.size(), n2 = s2.size();
if (n1 > n2)
return 0;
for (int i = 0; i < n1; ++i) {
++count[s1[i] - 'a'];
--count[s2[i] - 'a'];
}
if (allZero(count))
return 1;
for (int i = n1; i < n2; ++i) {
--count[s2[i] - 'a'];
++count[s2[i - n1] - 'a'];
if (allZero(count))
return 1;
}
return 0;
}
bool allZero(int arr[]) {
for (int i = 0; i < 26; ++i)
if (arr[i] != 0)
return 0;
return 1;
}
};
// uglycode.cpp
class Solution2 {
public:
bool checkInclusion(string s1, string s2) {
int s1s[26] = {};
int n = s1.size(), off = 0;
for (int i = 0; i < n; ++i)
++s1s[s1[i] - 'a'];
for (int i = 0; i + off < s2.size();) {
if (s1s[s2[i + off] - 'a'] > 0) {
s1s[s2[i + off] - 'a']--;
--n;
++off;
if (n == 0)
return 1;
} else {
if (off > 0) {
s1s[s2[i] - 'a']++;
--off;
++n;
}
++i;
}
}
return 0;
}
};