此代码(改编自前缀-后缀代码)对于较大的语料库非常慢:
s1 = ' gaf dggeg' s2 = 'ada gaf rd'
输出: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
任何有效替代方案的选择?