459. 重复的子字符串
作者:互联网
题目描述
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
原题请参考链接https://leetcode-cn.com/problems/repeated-substring-pattern/
题解
方法一 【kmp算法】
class Solution:
def get_next(self,s):
l = len(s)
j = 0
pnext = [0] * l
i = 1
while i<l:
if s[i] == s[j]:
pnext[i] = j+1
j += 1
i += 1
elif j != 0:
j = pnext[j-1]
else:
pnext[i] = 0
i += 1
return pnext
def repeatedSubstringPattern(self, s: str) -> bool:
if len(s) == 0:
return False
pnext = self.get_next(s)
l = len(s)
if pnext[-1] != 0 and l % (l-pnext[-1]) == 0:
return True
return False
标签:459,return,重复,self,len,next,字符串,pnext 来源: https://www.cnblogs.com/bladers/p/14482973.html