VOOZH about

URL: https://www.geeksforgeeks.org/dsa/anagram-substring-search-or-search-for-all-permutations-set-2/

⇱ Anagram Substring Search (Or Search for all permutations) | Set 2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Anagram Substring Search (Or Search for all permutations) | Set 2

Last Updated : 23 Jul, 2025

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 6

Input: txt = "AAABABAA", pat = "AABA"
Output: [0, 1, 4]
Explanation: "AAAB" is at 0, "AABA" at 5 and "ABAA" at 6

We previously covered the naive approach to solving this problem in our last article. In this article, we will explore an optimized method using hashing and sliding window technique.

Hashing and Sliding Window - O(256 * (n - m) + m) Time & O(m + 256) Space

The idea is to use two count arrays: 

  1. The first count array (say, countP) store frequencies of characters in pattern. 
  2. The second count array (say, countTW) stores frequencies of characters in current window of text. And we can get the count for next window by reducing frequency of last character of current window and increasing frequency of next character.

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).

Illustration:

Following are steps of this algorithm. 

  • 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[].
  • Now run a loop from i = m to n-1. Do following in loop. 
    • If the two count arrays are identical, we found an occurrence. 
    • Increment count of current character of text in countTW[] 
    • Decrement count of first character in previous window in countWT[]
  • The last window is not checked by above loop, so explicitly check it.

Output
0 5 6 

Time Complexity: O(256 * (n - m) + m)
Auxiliary space: O(m + 256)

If we look closely, we can see that a similar idea is used in the Rabin Karp Algorithm (Rolling Hash). 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.

Comment