Thomas
Thomas

Reputation: 123

How to find all overlaps of two strings in python

I have two strings, for example 'bbb', and 'bbab', and I want to find all of the overlaps between them (which in this case would be 'bbbbab', 'bbbab', and 'bbabbb'). Is there a python program in the documentation that does this?

Upvotes: 0

Views: 3361

Answers (1)

pentadecagon
pentadecagon

Reputation: 4847

There is no such library function, but you can do it like this:

def overlaps1( a, b ):
        for i in range( 1, min( len(a), len(b) ) ):
                if a[-i:] == b[:i]:
                        print( a + b[i:] )

def overlaps2( a, b ):
        overlaps1(a,b)
        overlaps1(b,a)

overlaps2( 'bbb', 'bbab' )

Upvotes: 3

Related Questions