1
class Solution:
2
def repeatedStringMatch(self, A: str, B: str) -> int:
3
if len(A) >= len(B):
4
if B in A:
5
return 1
6
elif B in A * 2:
7
return 2
8
else:
9
return -1
10
prefix = max(0, B.find(A)) # prefix -- length of A1
11
repeat, postfix = divmod(len(B) - prefix, len(A)) # postfix -- length of A2
12
repeat += bool(prefix) + bool(postfix)
13
if B in A * repeat:
14
return repeat
15
else:
16
return -1

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0