-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutation In String.cpp
46 lines (40 loc) · 1 KB
/
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
#include <iostream>
#include <unordered_map>
class Solution {
public:
bool checkInclusion(std::string s1, std::string s2) {
if (s1.size() > s2.size()) {
return false;
}
std::unordered_map<char, int> count;
for (char c : s1) {
count[c]++;
}
for (int i = 0; i < s1.size(); ++i) {
count[s2[i]]--;
}
bool valid = true;
for (const auto &pair : count) {
if (pair.second != 0) {
valid = false;
}
}
if (valid) {
return true;
}
for (int i = s1.size(); i < s2.size(); ++i) {
count[s2[i - s1.size()]]++;
count[s2[i]]--;
bool valid = true;
for (const auto &pair : count) {
if (pair.second != 0) {
valid = false;
}
}
if (valid) {
return true;
}
}
return false;
}
};