Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.0 kB
12
Indexable
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

bool compareArrays(int arr1[], int arr2[]) {
    for (int i = 0; i < 256; i++) {
        if (arr1[i] != arr2[i]) {
            return false;
        }
    }
    return true;
}

void findAnagramOccurrences(const string& text, const string& pattern) {
    int n = text.length();
    int m = pattern.length();

    int countPattern[256] = {0};
    int countWindow[256] = {0};

    for (int i = 0; i < m; i++) {
        countPattern[pattern[i]]++;
        countWindow[text[i]]++;
    }

    for (int i = m; i < n; i++) {
        if (compareArrays(countPattern, countWindow)) {
            cout << i - m << endl;
        }

        countWindow[text[i]]++;
        countWindow[text[i - m]]--;
    }

    if (compareArrays(countPattern, countWindow)) {
        cout << n - m << endl;
    }
}

int main() {
    string text, pattern;
    cin >> text >> pattern;

    findAnagramOccurrences(text, pattern);

    return 0;
}