# -*- coding:utf-8 -*-
# Author: Lucy
"""
输入:
"ababc"
返回值:
4
说明:
abab为最长的重复字符子串,长度为4
示例2
输入:
"abcab"
返回值:
0
说明:
该字符串没有重复字符子串
标准实现的方法:
"""
class Solution:
def solve(self , a ):
# write code here
def cf(s):
l=len(s)
mid=l//2
if l % 2==1:
return 0
elif s[:mid]==s[mid:]:
return l
else:
return 0
max=0
lena=len(a)
for i in range(lena):
for j in range(i,lena):
if max<cf(a[i:j]):
max=cf(a[i:j])
return max
if __name__ == '__main__':
ss = Solution()
str = "abab"
print(ss.solve(str))
"""
自己实现的写法
class Solution:
def solve(self , str) -> int:
# write code here
ll = []
for i in range(len(str)):
for j in range(i + 1, len(str) + 1):
if len(str[i:j]) % 2 == 0:
ll.append(str[i:j])
new_ll = sorted(ll, key=lambda t: len(t))
res = []
for k in new_ll:
for f in range(len(k)):
mid = int(len(k) / 2) # 除法运算这里有坑
# print(mid)
if k[:mid] == k[mid:]:
res.append(k)
break
res_ll = sorted(res, key=lambda f: len(f))
# print("res_ll", res_ll)
if len(res_ll) == 0:
return 0
else:
rr = len(res_ll[-1])
# print("******************")
# print(rr)
return rr
"""
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/274490.html