This code (adapted from a Prefix-Suffix code) is quite slow for larger corpora:
s1 = 'gafdggeg' s2 = 'adagafrd'
Output: gaf
def pref_also_substr(s):
n = len(s)
for res in range(n, 0, -1):
prefix = s[0: res]
if (prefix in s1):
return res
# if no prefix and string2 match occurs
return 0
Any option for an efficient alternative?