-
Notifications
You must be signed in to change notification settings - Fork 0
/
#111.cpp
53 lines (44 loc) · 1.06 KB
/
#111.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
49
50
51
52
53
#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
using namespace std;
bool is_anagram(int a[], int b[]) {
for (int i = 0; i < 256; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
vector<int> anagram_substrings(string s, string sub) {
vector<int> anagram_indices;
int sub_freq[256] = {0};
int curr_freq[256] = {0};
for (char c : sub) {
sub_freq[c] += 1;
}
int left = 0;
int right = 0;
while(right < sub.length()) {
curr_freq[s[right++]] += 1;
}
right--;
while(right < s.length()) {
if (is_anagram(sub_freq, curr_freq)) anagram_indices.push_back(left);
curr_freq[s[left++]] -= 1;
curr_freq[s[ min(++right, int(s.length()-1)) ]] += 1;
}
return anagram_indices;
}
int main() {
string inp;
while (cin >> inp) {
string s = inp;
cin >> inp;
string sub = inp;
vector<int> anagrams = anagram_substrings(s, sub);
for (int i : anagrams) {
cout << i << ",";
}
cout << endl;
}
}