Skip to content

Commit 5fd56b7

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 97ff3af commit 5fd56b7

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
vector<int> findAnagrams(string s, string p) {
8+
int count[128] = { 0 };
9+
for (char c : p) {
10+
count[c]++;
11+
}
12+
13+
vector<int> res;
14+
int l = 0, r = 0, len = 0;
15+
while (r < s.length()) {
16+
if (--count[s[r++]] >= 0) {
17+
len++;
18+
}
19+
20+
if (r - l >= p.length()) {
21+
if (len == p.length()) {
22+
res.push_back(l);
23+
}
24+
if (++count[s[l++]] > 0) {
25+
len--;
26+
}
27+
}
28+
}
29+
30+
return res;
31+
}
32+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
bool checkInclusion(string s1, string s2) {
8+
int count[128] = { 0 };
9+
for (char c : s1) {
10+
count[c]++;
11+
}
12+
13+
int l = 0, r = 0, len = 0;
14+
while (r < s2.length()) {
15+
if (--count[s2[r++]] >= 0) {
16+
len++;
17+
}
18+
19+
if (r - l >= s1.length()) {
20+
if (len == s1.length()) {
21+
return true;
22+
}
23+
if (++count[s2[l++]] > 0) {
24+
len--;
25+
}
26+
}
27+
}
28+
29+
return false;
30+
}
31+
};

0 commit comments

Comments
 (0)