C# Program for Anagram Substring Search (Or Search for all permutations)
Last Updated :
23 Jul, 2025
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] and its permutations (or anagrams) in txt[]. You may assume that n > m.
Expected time complexity is O(n)
Examples:
1) Input: txt[] = "BACDGABCDA" pat[] = "ABCD"
Output: Found at Index 0
Found at Index 5
Found at Index 6
2) Input: txt[] = "AAABABAA" pat[] = "AABA"
Output: Found at Index 0
Found at Index 1
Found at Index 4
We strongly recommend that you click here and practice it, before moving on to the solution.
A simple idea is to modify
Rabin Karp Algorithm. For example we can keep the hash value as sum of ASCII values of all characters under modulo of a big prime number. For every character of text, we can add the current character to hash value and subtract the first character of previous window. This solution looks good, but like standard Rabin Karp, the worst case time complexity of this solution is O(mn). The worst case occurs when all hash values match and we one by one match all characters.
We can achieve O(n) time complexity under the assumption that alphabet size is fixed which is typically true as we have maximum 256 possible characters in ASCII. The idea is to use two count arrays:
1) The first count array store frequencies of characters in pattern.
2) The second count array stores frequencies of characters in current window of text.
The important thing to note is, time complexity to compare two count arrays is O(1) as the number of elements in them are fixed (independent of pattern and text sizes). Following are steps of this algorithm.
1) Store counts of frequencies of pattern in first count array
countP[]. Also store counts of frequencies of characters in first window of text in array
countTW[].
2) Now run a loop from i = M to N-1. Do following in loop.
.....a) If the two count arrays are identical, we found an occurrence.
.....b) Increment count of current character of text in countTW[]
.....c) Decrement count of first character in previous window in countWT[]
3) The last window is not checked by above loop, so explicitly check it.
C#
// C# program to search all anagrams
// of a pattern in a text
using System;
class GFG {
public const int MAX = 256;
// This function returns true if
// contents of arr1[] and arr2[]
// are same, otherwise false.
public static bool compare(char[] arr1,
char[] arr2)
{
for (int i = 0; i < MAX; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
// This function search for all
// permutations of pat[] in txt[]
public static void search(string pat,
string txt)
{
int M = pat.Length;
int N = txt.Length;
// countP[]: Store count of all
// characters of pattern
// countTW[]: Store count of current
// window of text
char[] countP = new char[MAX];
char[] countTW = new char[MAX];
for (int i = 0; i < M; i++) {
(countP[pat[i]])++;
(countTW[txt[i]])++;
}
// Traverse through remaining
// characters of pattern
for (int i = M; i < N; i++) {
// Compare counts of current window
// of text with counts of pattern[]
if (compare(countP, countTW)) {
Console.WriteLine("Found at Index " + (i - M));
}
// Add current character to
// current window
(countTW[txt[i]])++;
// Remove the first character of
// previous window
countTW[txt[i - M]]--;
}
// Check for the last window in text
if (compare(countP, countTW)) {
Console.WriteLine("Found at Index " + (N - M));
}
}
// Driver Code
public static void Main(string[] args)
{
string txt = "BACDGABCDA";
string pat = "ABCD";
search(pat, txt);
}
}
// This code is contributed
// by Shrikant1
Output:
Found at Index 0
Found at Index 5
Found at Index 6
Please refer complete article on
Anagram Substring Search (Or Search for all permutations) for more details!
Similar Reads
C# Program for Naive algorithm for Pattern Searching Write a C# program for a given text string with length n and a pattern with length m, the task is to print all occurrences of the pattern in text.Note: You may assume that n > m. Examples: Input: text = âTHIS IS A TEST TEXTâ, pattern = âTESTâOutput: Pattern found at index 10 Input: text = âAABAAC
2 min read
C# Program to print all permutations of a given string A permutation also called an "arrangement number" or "order," is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation. Source: Mathword(https://mathworld.wolfram.com/Permutation.html) Below are the permutations
3 min read
C Program for Anagram Substring Search (Or Search for all permutations) Write a C program for a given text txt[0..n-1] and a pattern pat[0..m-1], the task is to prints all occurrences of pat[] and its permutations (or anagrams) in txt[]. You may assume that n > m. Note: Expected time complexity is O(n) Examples: Input: txt[] = "BACDGABCDA" pat[] = "ABCD"Output: Found
6 min read
Anagram Substring Search (Or Search for all permutations) Given a text txt and a pattern pat of size n and m respectively, the task is to find all occurrences of pat and its permutations (or anagrams) in txt. You may assume n > m.Examples: Input: txt = "BACDGABCDA", pat = "ABCD"Output: [0, 5, 6]Explanation: "BACD" is at 0, "ABCD" at 5 and "BCDA" at 6Inp
7 min read
Anagram Substring Search (Or Search for all permutations) | Set 2 Given a text txt and a pattern pat of size n and m respectively, the task is to find all occurrences of pat and its permutations (or anagrams) in txt. You may assume n > m.Examples: Input: txt = "BACDGABCDA", pat = "ABCD"Output: [0, 5, 6]Explanation: "BACD" is at 0, "ABCD" at 5 and "BCDA" at 6Inp
7 min read
Java Program for Anagram Substring Search (Or Search for all permutations) Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] and its permutations (or anagrams) in txt[]. You may assume that n > m. Expected time complexity is O(n) Examples: 1) Input: txt[] = "BACDGABCDA" pat[] = "ABCD"
4 min read