# C Program for Anagram Substring Search

In this problem, we are given two string one text of size n and other a pattern of size m. Our task is to create a program for Anagram substring search.

Here, we have to find all the occurrence of pattern and all its permutations (anagrams) in the text.

Let’s take an example to understand the problem,

## Input

text = “xyztrwqyzxfg” pattern = “xyz”

## Output

Found at index 0
Found at index 7

To solve this problem, we will have to use an algorithm similar to the Rabin Karp algorithm which is used to check for anagram occurrence by adding the ASCII values of all characters under modulo of a number. and then using a window of characteristics sets and matching the sum.

The solution will require two arrays that will be to store the frequencies of characters in the window of the text as well as the matching pattern. Then we will slide the window by one and match the character frequencies for each widow and print of the matching pattern.

## Program for Anagram Substring Search

//Program for Anagram Substring Search

## Example

Live Demo

#include <cstring>
#include <iostream>
#define MAX 256
using namespace std;
bool matchPattern(char arr1[], char arr2[]){
for (int i = 0; i < MAX; i++)
if (arr1[i] != arr2[i])
return false;
return true;
}
void anagramSearch(char* pattern, char* text){
int M = strlen(pattern);
int N = strlen(text);
char patternArray[MAX] = { 0 }, textArray[MAX] = { 0 };
for (int i = 0; i < M; i++) {
(patternArray[pattern[i]])++;
(textArray[text[i]])++;
}
for (int i = M; i < N; i++) {
if (matchPattern(patternArray, textArray))
printf("Pattern found at index value : %d", (i-M));
(textArray[text[i]])++;
textArray[text[i - M]]--;
}
if (matchPattern(patternArray, textArray))
printf("Pattern found at index value: %d", (N-M));
}
int main() {
char text[] = "xyztrwqyzxfg";
char pattern[] = "xyz";
printf("Searching Anagram pattern in the string ");
anagramSearch(pattern, text);
return 0;
}

## Output

Searching Anagram pattern in the string
Pattern found at index value: 0
Pattern found at index value: 7