<@U0AAQKT9Q>: Regarding a): That should be fairly...
# stdlib
c
@ilya.gorbunov: Regarding a): That should be fairly easy to do. b): I don't think this can be achieved in a good way using Knuth-Morris-Pratt, except for just running it with pattern after pattern. It's using the prefix function to avoid unnecessary comparisons, which makes this O(n), but I can't think of a way to efficiently support multiple patterns using that method. An abstract use case would be finding all occurences of a short pattern of length
k
in a long text of length
n
, which would be O(nk) with
indexOf
, but is O(n) with
occurencesOf
. A more concrete use case: Suppose we have a large number of texts (like a search engine) we would like to run a search query against. With
occurencesOf
, we could get all relevant parts of the text very efficiently, maybe get a substring of the occurence 100 chars before and after, obtaining a summary of where the word occurs.