In [1]:
def suffixPrefixMatch(str1, str2, min_overlap):
''' Returns length of longest suffix of str1 that is prefix of
str2, as long as that suffix is at least as long as min_overlap. '''
if len(str2) < min_overlap: return 0
str2_prefix = str2[:min_overlap]
str1_pos = -1
while True:
str1_pos = str1.find(str2_prefix, str1_pos + 1)
if str1_pos == -1: return 0
str1_suffix = str1[str1_pos:]
if str2.startswith(str1_suffix): return len(str1_suffix)
In [2]:
# overlap is 'more'
suffixPrefixMatch('a little more', 'more than kin', min_overlap=3)
Out[2]:
In [3]:
# len('more') < 5 so overlap won't be found
suffixPrefixMatch('a little more', 'more than kin', min_overlap=5)
Out[3]: