counting number of (overlapping) occurances

Paul Rubin http
Fri Mar 10 01:13:11 EST 2006


"John" <weekender_ny at yahoo.com> writes:
> This works but is a bit slow, I guess I'll have to live with it.
> Any chance this could be sped up in python?

Whoops, I meant to say:

  len([1 for i in xrange(len(s1)) if s1.startswith(s2,i)])

That avoids creating a lot of small strings.

If s1 is large you may want to use fancy algorithms like Boyer-Moore
string search.  If s2 is also large, you could have some kind of
finite state machine scan through s1 so it recognizes overlapping
occurrences of s2 in parallel.  There might be some way to combine
that with Boyer-Moore.



More information about the Python-list mailing list