-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0567-permutation-in-string.cpp
48 lines (42 loc) · 1.08 KB
/
0567-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
/*
Given 2 strings, return true if s2 contains permutation of s1
Ex. s1 = "ab", s2 = "eidbaooo" -> true, s2 contains "ba"
Sliding window, expand + count down char, contract + count up char
Time: O(n)
Space: O(1)
*/
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int m = s1.size();
int n = s2.size();
if (m > n) {
return false;
}
vector<int> count(26);
for (int i = 0; i < m; i++) {
count[s1[i] - 'a']++;
count[s2[i] - 'a']--;
}
if (isPermutation(count)) {
return true;
}
for (int i = m; i < n; i++) {
count[s2[i] - 'a']--;
count[s2[i - m] - 'a']++;
if (isPermutation(count)) {
return true;
}
}
return false;
}
private:
bool isPermutation(vector<int>& count) {
for (int i = 0; i < 26; i++) {
if (count[i] != 0) {
return false;
}
}
return true;
}
};