-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP567.cpp
59 lines (55 loc) · 1.53 KB
/
P567.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
57
58
59
#include "header.h"
#include <array>
#include <cstddef>
class Solution {
public:
array<int, 26> letter1, letter2, t;
bool checkInclusion(string s1, string s2) {
letter1.fill(0);
letter2.fill(0);
for (const auto &v:s1) {
letter1[v-'a']++;
}
for (const auto &v:s2) {
letter2[v-'a']++;
}
for (size_t i=0; i<26; i++) {
if (letter2[i]<letter1[i])
return false;
}
size_t head,tail;
head = tail = 0;
t.fill(0);
while (tail<s2.size()) {
size_t c = s2[tail]-'a';
// cout << head << " "<< tail << endl;
if (t[c] < letter1[c]) {
t[c]++;
tail++;
if (tail-head == s1.size()) {
// cout << head << " "<< tail << endl;
return true;
}
}
else if (letter1[c]==0){
t.fill(0);
head=tail+1;
tail=tail+1;
}
else {
size_t cc = s2[head]-'a';
t[cc]--;
head++;
if (head>tail) tail = head;
}
}
return false;
}
};
int main() {
cout << Solution().checkInclusion("ab", "eidbaooo") << endl;
cout << Solution().checkInclusion("ab", "eidboaooo") << endl;
cout << Solution().checkInclusion("adc", "dcda") << endl;
cout << Solution().checkInclusion("aa", "aaaaaa") << endl;
return 0;
}