Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program for Anagram Substring Search
In this problem, we are given two strings: one text of size n and another pattern of size m. Our task is to create a program for anagram substring search.
Here, we have to find all occurrences of the pattern and all its permutations (anagrams) in the text. An anagram is a word formed by rearranging the letters of another word.
Let's take an example to understand the problem −
Input
text = "xyztrwqyzxfg" pattern = "xyz"
Output
Found at index 0 Found at index 7
Algorithm
To solve this problem, we use a sliding window approach similar to the Rabin Karp algorithm. The solution uses two character frequency arrays to store the frequencies of characters in the pattern and the current window of text. We slide the window by one position and match character frequencies for each window.
Example
The following program demonstrates anagram substring search using character frequency comparison −
#include <stdio.h>
#include <string.h>
#define MAX 256
int matchPattern(int arr1[], int arr2[]) {
for (int i = 0; i < MAX; i++)
if (arr1[i] != arr2[i])
return 0;
return 1;
}
void anagramSearch(char* pattern, char* text) {
int M = strlen(pattern);
int N = strlen(text);
int patternArray[MAX] = {0}, textArray[MAX] = {0};
/* Initialize frequency arrays for pattern and first window */
for (int i = 0; i < M; i++) {
(patternArray[pattern[i]])++;
(textArray[text[i]])++;
}
/* Slide the window over text and check for anagram match */
for (int i = M; i < N; i++) {
if (matchPattern(patternArray, textArray))
printf("Pattern found at index: %d<br>", (i - M));
/* Add new character and remove old character from window */
(textArray[text[i]])++;
textArray[text[i - M]]--;
}
/* Check for the last window */
if (matchPattern(patternArray, textArray))
printf("Pattern found at index: %d<br>", (N - M));
}
int main() {
char text[] = "xyztrwqyzxfg";
char pattern[] = "xyz";
printf("Searching anagram pattern in the string:<br>");
printf("Text: %s<br>", text);
printf("Pattern: %s<br><br>", pattern);
anagramSearch(pattern, text);
return 0;
}
Output
Searching anagram pattern in the string: Text: xyztrwqyzxfg Pattern: xyz Pattern found at index: 0 Pattern found at index: 7
How It Works
- The algorithm uses a frequency array of size 256 to count ASCII characters.
- It maintains two arrays: one for pattern frequencies and one for current window frequencies.
- The sliding window technique moves through the text, updating frequencies efficiently.
- Time complexity: O(n) where n is the length of text.
- Space complexity: O(1) as we use fixed size arrays.
Conclusion
The anagram substring search efficiently finds all permutations of a pattern within a text using character frequency comparison. This sliding window approach provides optimal performance for pattern matching problems.
