我去学校工作时学习python。本质上,我需要在字符串列表中找到最长的重复子字符串,如下所示。我一直在阅读这篇文章,并了解我应该做什么。
到目前为止,我的实现如下:
def long_rptr_subString(testList):
longSubstring = ''
if len(testList) > 1 and len(testList[0]) > 0:
for i in range(len(testList[0])):
for j in range(len(testList[0])-i+1):
if j > len(longSubstring) and all(testList[0][i:i+j] in x for x in testList):
longSubstring = testList[0][i:i+j]
return longSubstring
现在,当我调用我的函数时,假设['slide', 'glidb', 'flidt', 'cridz', 'bidr']
我得到了'id'
作为最长子字符串的正确结果。
但是,当我通过列表时,['slide', 'glidb', 'flidt', 'cridz', 'bidr', 'balh', 'tejka', 'djakljskdl', 'blah', 'blah', 'blah']
我没有得到任何返回结果。我应该作为我最长的子字符串返回'blah'
,但我还没有找到实现这一目标的方法。似乎我只能在列表的所有项目中匹配子字符串。我如何修改我的代码/逻辑以获得多次出现的最长子字符串?
谢谢你。