-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
38 lines (34 loc) · 996 Bytes
/
solution.py
File metadata and controls
38 lines (34 loc) · 996 Bytes
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
34
35
36
37
38
class Solution(object):
def repeatedStringMatch(self, A, B):
"""
:type A: str
:type B: str
:rtype: int
"""
length_A = len(A)
length_B = len(B)
i = j = 0
nxt = []
# get next array
while i < length_B:
if i == 0 or (B[i] != B[j] and j == 0):
nxt.append(0)
elif B[i] == B[j]:
nxt.append(j + 1)
j += 1
elif B[i] != B[j] and j > 0:
j = nxt[j - 1]
continue
i += 1
i = j = 0
while j < length_B:
if A[i % length_A] == B[j]:
i += 1
j += 1
elif j > 0:
j = nxt[j - 1]
elif j == 0:
i += 1
if i >= length_A + length_B:
return -1
return i / length_A + 1 if i % length_A > 0 else i / length_A