Python Program For Array Rotation
Python Program For Array Rotation
“Given a text and a pattern, we need to print all occurrences of pattern and its permutations (or
anagrams) in text.”
Variable notations
Please note that the length of the pattern will always be less than or equal to that of length of input string.
We can see that this problem is the same as the pattern matching problem with the major difference being
that instead of finding a pattern we also have to search for its possible permutation(anagrams).
There are several algorithms for finding a pattern in a string but we shall be looking into the Naive
approach to the problem.
As we can see that the main problem is to find all the possible permutations this is why we shall first sort
the pattern to be matched and compare it with the sorted slices of text. This makes sure that all possible
anagrams are matched without any error.
Next comes the problem of searching, as we discussed earlier we will be using a naive approach. That is
we shall first create a window size, that will be equal to the length of the pattern. We will then look for
the pattern in the text by using the sliding method. We first take out a slice of text from the original text
whose size will be equal to the size of the window, and compare them, if they match we print the index
and if not than we will move the window by 1.
Please note that the last possible window can only be from (N - M) to N - 1 this is why we will iterate
over to that point only.
Algorithm
We will create a function “search” that will take the text and the pattern as input.
● Sort the pattern by making it a list, sorting it, and joining the characters to form a string again.
● Iterate using for loop from the index 0 to N - M + 1
● Create a temporary slice of length = M from index “ i to i + M”
● Sort the newly created temp
● Check whether temp is equal to the pattern
1
Harshit Sachan
Example
lenPattern = len(pattern)
for i in range(len(text) - lenPattern + 1):
temp = "".join([str(x) for x in sorted(list(text[i : i+lenPattern]))])
if temp == pattern:
print("Pattern found at index: ", i)
if __name__ == "__main__":
text = input("Enter the text: ")
pattern = input("Enter the pattern to search: ")
search(text, pattern)
Output
Explanation
The above program takes a text and a pattern as input, AABAACAADAABAAB, and BAAA
respectively. We then sort the pattern to cover all possible anagrams, AAAB. After which we start
comparing using the sliding window method and print out all the indices where the pattern is found, 0, 1,
9, 10, 12 in this case.
2
Harshit Sachan
The above program takes N * M iteration to match the string and N * log(N) time each time to sort the
temp slice of text. So we can say that its time complexity is around O( (N-M+1)*(M+MlogM+M) ).
The main problem with this approach is that it uses a Naive approach for searching and in the worst case
it performs poorly than other algorithms.
We can also use the KMP algorithm to search for patterns in the string. It is a better approach than the
Naive approach. It is based on the idea of using the pattern to make sure we never backtrack
unnecessarily when we do not have to. It stores the pattern in the form of a table where we map the values
to indexes of the same character if we have seen it else to 0. This makes sure that if there are any similar
substrings in the pattern we use them so that we do not need to backtrack.
For example, if we have a pattern like “ABABAA” it will have mapping to “001233” this way whenever
we find a character in the text that does not match instead of going to the beginning we go to the index
pointed by the character that was mismatched.
The time complexity for this algorithm is O(M + N) which is far better than the Naive algorithm.
Another algorithm we can use is called “Rabin Carp” Algorithm it has the time complexity of O(M) and
uses the idea of hash maps and frequencies to search for patterns.
Conclusion
We saw how can we use the Naive pattern searching algorithm in python to find a pattern and its
anagrams in a given string. We also discussed solutions that perform better than the Naive algorithm.