# LeetCode: 466. 统计重复个数¶

## 1、题目描述¶

输入：
s1 ="acb",n1 = 4
s2 ="ab",n2 = 2

2


## 2、解题思路¶

• 寻找重复规则

s1:   abcd
s2:   dc

abcd abcd abcd abcd abcd abcd
d   cd   cd   cd   cd   cd
3   23   23   23   23   23               s1下标
0   10   10   10   10   10               s2下标


class Solution:
def getMaxRepetitions(self, s1: str, n1: int, s2: str, n2: int) -> int:

if not set(s2).issubset(set(s1)):
return 0

mapping = {}

length_s1 = len(s1)
length_s2 = len(s2)

pos_s1 = 0
pos_s2 = 0

while pos_s1 < length_s1 * n1:
while s1[pos_s1 % length_s1] != s2[pos_s2 % length_s2]:
pos_s1 += 1

if pos_s1 >= length_s1 * n1:
break
if (pos_s1 % length_s1, pos_s2 % length_s2) not in mapping:
mapping[(pos_s1 % length_s1, pos_s2 % length_s2)] = [pos_s1, pos_s2]
else:
pos1, pos2 = mapping[(pos_s1 % length_s1, pos_s2 % length_s2)]
circle = (length_s1 * n1 - pos1) // (pos_s1 - pos1)
pos_s1 = pos1 + circle * (pos_s1 - pos1)
pos_s2 = pos2 + circle * (pos_s2 - pos2)
if pos_s1 < length_s1 * n1:
pos_s1 += 1
pos_s2 += 1

return pos_s2 // (length_s2 * n2)