今天是代码随想录算法训练营第八天。学习了KMP,之后还得要再去看看。
写了力扣:459. 重复的子字符串
代码如下:
# (版本一) 前缀表 减一
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
if len(s) == 0:
return False
nxt = [0] * len(s)
self.getNext(nxt, s)
if nxt[-1] != -1 and len(s) % (len(s) - (nxt[-1] + 1)) == 0:
return True
return False
def getNext(self, nxt, s):
nxt[0] = -1
j = -1
for i in range(1, len(s)):
while j >= 0 and s[i] != s[j+1]:
j = nxt[j]
if s[i] == s[j+1]:
j += 1
nxt[i] = j
return nxt```
整体而言,KMP还需要再看看。接下来是栈和队列章节了。