I'm trying to answer this homework question: Find all occurrences of a pattern in a string. Different occurrences of a substring can overlap with each other.
Sample 1.
Input:
TACG
GT
Output:
Explanation: The pattern is longer than the text and hence has no occurrences in the text.
Sample 2.
Input:
ATA
ATATA
Output:
0 2
Explanation: The pattern appears at positions 1 and 3 (and these two occurrences overlap each other).
Sample 3.
ATAT
GATATATGCATATACTT
Output:
1 3 9
Explanation: The pattern appears at positions 1, 3, and 9 in the text.
The answer I'm submitting is this one:
def all_indices(text, pattern):
i = text.find(pattern)
while i >= 0:
print(i, end=' ')
i = text.find(pattern, i + 1)
if __name__ == '__main__':
text = input()
pattern = input()
all_indices(text, pattern)
However, this code is failing the final test cases:
Failed case #63/64: time limit exceeded (Time used: 7.98/4.00, memory used: 77647872/536870912.)
The online judge knows I'm sending the answer in Python, and has different time limits for different languages.
I have searched quite a bit for other answers and approaches: regexes, suffix trees, Aho-Corasick... but so far all of them underperform this simple solution (maybe because find
is implemented in C?).
So my question is: are there ways to do this task faster?