如何在Python中的字符串中找到最长的重复序列?
您可以使用defaultdict对从输入字符串中每个位置开始的每个子字符串进行计数。getsubs方法是一种生成器方法,每次调用时都会产生一个较小的子字符串。
示例
from collections import defaultdict
def getsubs(loc, s):
substr = s[loc:]
i = -1
while(substr):
yield substr
substr = s[loc:i]
i -= 1
def longestRepetitiveSubstring(r):
occ = defaultdict(int)
# tally all occurrences of all substrings
for i in range(len(r)):
for sub in getsubs(i,r):
occ[sub] += 1
# filter out all sub strings with fewer than 2 occurrences
filtered = [k for k,v in occ.items() if v >= 2]
if filtered:
maxkey = max(filtered, key=len) # Find longest string
return maxkey
else:
raise ValueError("no repetitions of any substring of '%s' with 2 or more occurrences" % (r))
longestRepetitiveSubstring("hellopeople18654randomtexthellopeoplefromallaroundthe world")输出结果
这将给出输出:
'hellopeople'
热门推荐
10 歌词朋友结婚祝福语简短
11 毕业祝福语贺词大全简短
12 高考同学留言祝福语简短
13 邮政考试祝福语简短励志
14 送你妹妹新婚祝福语简短
15 职场同事离婚祝福语简短
16 除夕时刻祝福语大全简短
17 老师简短祝福语有哪些
18 婚礼父母在家祝福语简短