
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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
#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
- Related Articles
- Python Program for Anagram Substring Search
- Java Program for Anagram Substring Search
- Anagram Substring Search using Python
- Anagram Pattern Search
- Java Program to search for a Substring from a specified index
- C/C++ Program for Linear Search?
- Recursive function to do substring search in C++
- C Program for Binary Search (Recursive and Iterative)?
- C++ Program to Search for an Element in a Binary Search Tree
- C++ Program to Implement a Binary Search Algorithm for a Specific Search Sequence
- Python Program for Binary Search
- Python Program for Linear Search
- 8085 program for Binary search
- Java Program for Binary Search (Recursive)
- Binary Search in C++ program?
