This is purely out of curiosity. I was browsing through an article comparing various string search algorithms and noticed they were all designed to find the first matching substring. This got me thinking... What if I wanted to find all occurrences of a substring?
I'm sure I could create a loop that used a variant of KMP or BM and dumped each found occurrence into an array but this hardly seems like it would be the fastest.
Wouldn't a divide and conquer algorithm be superior?
For instance lets say your looking for the sequence "abc" in a string "abbcacabbcabcacbccbabc".
- On the first pass find all occurrences of the first character and store their positions.
- On each additional pass use the positions from the preceding pass to find all occurrences of next character, reducing the candidates for the next pass with each iteration.
Considering the ease with which I came up with this idea I assume someone already came up with it and improved upon it 30 years ago.