-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
31 lines (27 loc) · 774 Bytes
/
solution.cpp
File metadata and controls
31 lines (27 loc) · 774 Bytes
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
class Solution
{
public:
bool checkInclusion(string s1, string s2)
{
if (s1.length() > s2.length())
return false;
vector<int> s1Count(26, 0), s2Count(26, 0);
// Count frequencies of s1 and the first window in s2
for (int i = 0; i < s1.length(); ++i)
{
s1Count[s1[i] - 'a']++;
s2Count[s2[i] - 'a']++;
}
// Slide the window over s2
for (int i = 0; i < s2.length() - s1.length(); ++i)
{
if (s1Count == s2Count)
return true;
// Update the window
s2Count[s2[i] - 'a']--;
s2Count[s2[i + s1.length()] - 'a']++;
}
// Check the last window
return s1Count == s2Count;
}
};