686.重复叠加字符串匹配
一、利用重复的循环性质
class Solution:
def repeatedStringMatch(self, a: str, b: str) -> int:
dup = "" #复制串
n = 0 #复制次数
while len(dup) < len(b):
dup += a
n += 1
dup += a #复制n+1次
idx = str.find(dup, b) #求匹配下标
if idx == -1:
return -1
else:
if idx + len(b) > n * len(a):
return n + 1
else:
return n最后更新于