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
Anagram Pattern Search
Anagrams are basically all permutations of a given string or pattern. This pattern searching algorithm is slightly different. In this case, not only the exact pattern is searched, it searches all possible arrangements of the given pattern in the text.
To solve this problem, we will divide the whole texts into several windows of length same as patterns. Then count on each character of the pattern is found and stored in an array. For each window, we also try to find the count array, then check whether they are matching or not.
The time Complexity of Anagram Pattern Search Algorithm is O(n).
Input and Output
Input: The main String “AABAACBABBCABAABBA”. The pattern “AABC”. Output: Anagram found at position: 2 Anagram found at position: 3 Anagram found at position: 4 Anagram found at position: 10
Algorithm
anagramSearch(text, pattern)
Input − The main string and the pattern
Output − All locations where pattern and it’s all anagrams are found.
Begin define patternFreq array and stringFreq array patLne := length of pattern stringLen := length of the text set all entries of patternFreq array to 0 for all characters present in pattern, do increase the frequency. done for i := 0 to iExample
#include#include #define LETTER 26 using namespace std; bool arrayCompare(int *array1, int *array2, int n) { for(int i = 0; i Output
Anagram found at position: 2 Anagram found at position: 3 Anagram found at position: 4 Anagram found at position: 10
