-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path466. Count The Repetitions.py
33 lines (27 loc) · 1.04 KB
/
466. Count The Repetitions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def getMaxRepetitions(self, s1: str, n1: int, s2: str, n2: int) -> int:
if not set(s2).issubset(set(s1)):
return 0
s1_count, s2_count = 0, 0
index = 0
recall = {}
while s1_count < n1:
for char in s1:
if char == s2[index]:
index += 1
if index == len(s2):
s2_count += 1
index = 0
s1_count += 1
if index in recall:
s1_prev, s2_prev = recall[index]
cycle_len = s1_count - s1_prev
cycle_count = (n1 - s1_prev) // cycle_len
s1_count = s1_prev + cycle_count * cycle_len
s2_count = s2_prev + cycle_count * (s2_count - s2_prev)
recall[index] = (s1_count, s2_count)
return s2_count // n2
# Example usage:
solution = Solution()
print(solution.getMaxRepetitions("acb", 4, "ab", 2)) # Output: 2
print(solution.getMaxRepetitions("acb", 1, "acb", 1)) # Output: 1