- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for Anagram Substring Search
In this article, we will learn about the solution to the problem statement given below.
Problem statement − Given a text and a pattern, we need to print all occurrences of pattern and its permutations (or anagrams) in text.
Now let’s observe the solution in the implementation below −
# maximum value MAX = 300 # compare def compare(arr1, arr2): for i in range(MAX): if arr1[i] != arr2[i]: return False return True # search def search(pat, txt): M = len(pat) N = len(txt) # countP pattern account # countTW text window count countP = *MAX countTW = *MAX for i in range(M): (countP[ord(pat[i]) ]) += 1 (countTW[ord(txt[i]) ]) += 1 # Traversal for i in range(M, N): # Compare current window and patten counts if compare(countP, countTW): print("Found at Index", (i-M)) # Add charcter to window (countTW[ ord(txt[i]) ]) += 1 # remove charcter from window (countTW[ ord(txt[i-M]) ]) -= 1 # Check for the last window if compare(countP, countTW): print("It is Found at Index : ", N-M) # main txt = "TUTORIALSPOINT" pat = "TOR" search(pat, txt)
Found at Index 2
All the variables are declared in the local scope and their references are seen in the figure above.
In this article, we have learned about how we can make a Python Program for Anagram Substring Search.
- Java Program for Anagram Substring Search
- C Program for Anagram Substring Search
- Anagram Substring Search using Python
- Anagram Pattern Search
- Python Program for Linear Search
- Python Program for Binary Search
- Java Program to search for a Substring from a specified index
- Anagram checking in Python program using collections.Counter()
- Write a program for Linear Search in Python
- 8085 program for Binary search
- Valid Anagram in Python
- C/C++ Program for Linear Search?
- Java Program for Binary Search (Recursive)
- Python Program for Depth First Binary Tree Search using Recursion
- SequenceMatcher in Python for Longest Common Substring.