-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00567-permutation_in_string.cpp
54 lines (40 loc) · 1.04 KB
/
00567-permutation_in_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
// 567: Permutation in String
// https://leetcode.com/problems/permutation-in-string/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
// SOLUTION
bool checkInclusion(string s1, string s2) {
if (s1.size() > s2.size()) return false;
vector<int> s1Count(26); for (auto c : s1) s1Count[c-'a']++;
vector<int> s2Count(26);
int i = 0;
int j = 0;
while (j < s2.size()) {
s2Count[s2[j]-'a']++;
if (j-i+1 == s1.size())
if (s1Count == s2Count)
return true;
if (j-i+1 < s1.size())
j++;
else {
s2Count[s2[i]-'a']--;
i++;
j++;
}
}
return false;
}
};
int main() {
Solution o;
// INPUT
string s1 = "ab";
string s2 = "eidbaooo";
// OUTPUT
auto result = o.checkInclusion(s1, s2);
cout<<(result ? "true" : "false")<<endl;
return 0;
}