![]() |
VOOZH | about |
To count the number of overlapping sub-strings in Python we can use the Re module. To get the indices we will use the re.finditer() method. But it returns the count of non-overlapping indices only.
Examples:
Input: String: "geeksforgeeksforgeeks" ; Pattern: "geeksforgeeks"
Output: [0, 8]
Explanation: The pattern is overlapping the string from 0th index to 12th index and again overlapping it from 8th index to 20th index. Hence, the output is the starting positions of overlapping i.e index 0 and index 8.
Input: String: "barfoobarfoobarfoobarfoobarfoo" ; Pattern: "foobarfoo"
Output: [3, 9,15, 21]
Explanation: The pattern is overlapping the string from index 3, 9 , 15 and 21.
This method returns the count of non-overlapping indices only from a string having multiple occurrences overlapping pattern. Below is a program depicting the use of finditer() method.
Output:
[0]
Therefore, to get the overlapping indices as well we need to do is escape out of the regular expressions in the pattern. The definition in the explicit function helps to select the characters in a partial way.
Approach:
Below is the implementation of the above approach:
Output:
[0, 8]
[3, 9, 15, 21]
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Another approach is to use the sliding window method
Here's a step-by-step algorithm for implementing the Python program to find indices of overlapping substrings using the sliding window method:
Define a function named overlapping_substring that takes in two parameters - string and pattern.
Initialize an empty list named result to store the indices of overlapping substrings.
Loop through the string using the range function with the length of string minus the length of pattern plus 1 as the upper limit. This is because the sliding window will only be valid until the last substring of length pattern.
Check if the current substring of string of length pattern starting at index i is equal to the pattern. If it is, append the index i to the result list.
Return the result list.
Define two example strings string1 and string2 along with their respective pattern substrings.
Call the overlapping_substring function with the string1 and pattern1 arguments and print the resulting list of indices.
Call the overlapping_substring function with the string2 and pattern2 arguments and print the resulting list of indices.
[0, 8] [3, 9, 15, 21]
The time complexity of this algorithm is O(n * m), where n is the length of string and m is the length of pattern. This is because we are looping through the string and comparing each substring of length pattern with the pattern. The auxiliary space complexity is also O(n), where n is the length of the string, because we are creating a new list to store the indices of overlapping substrings.
Overall, the sliding window method is an efficient way to find overlapping substrings in a string, especially when the length of the pattern is small compared to the length of the string.