-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-18-permutation_in_string.cpp
48 lines (40 loc) · 1.23 KB
/
day-18-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
// Problem link: https://leetcode.com/problems/permutation-in-string
#include <iostream>
#include <vector>
using namespace std;
// Time complexity => O(|s1| + |s2|)
// Space complexity => O(1)
vector<int> getCharCounts(string& str) {
vector<int> charCounts(26, 0);
for (char ch : str) {
charCounts[ch - 'a']++;
}
return charCounts;
}
bool areCharCountsEqual(vector<int>& charCounts1, vector<int>& charCounts2) {
for (int i = 0; i < 26; i++) {
if (charCounts1[i] != charCounts2[i]) return false;
}
return true;
}
bool checkInclusion(string s1, string s2) {
vector<int> s1CharCounts = getCharCounts(s1);
vector<int> s2CharCounts(26, 0);
int windowStart = 0;
for (int windowEnd = 0; windowEnd < (int)s2.length(); windowEnd++) {
char currChar = s2[windowEnd];
++s2CharCounts[currChar - 'a'];
int windowLen = windowEnd - windowStart + 1;
if (windowLen == (int)s1.length()) {
if (areCharCountsEqual(s1CharCounts, s2CharCounts)) {
return true;
}
char charAtWindowStart = s2[windowStart];
--s2CharCounts[charAtWindowStart - 'a'];
++windowStart;
}
}
return false;
}
int main() {
}