![]() |
VOOZH | about |
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.
The idea is to use two count arrays:
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.
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.