forked from vJechsmayr/PythonAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0686_Repeated_String_Match.py
More file actions
27 lines (23 loc) · 916 Bytes
/
Copy path0686_Repeated_String_Match.py
File metadata and controls
27 lines (23 loc) · 916 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
class Solution(object):
def repeatedStringMatch(self, a, b):
"""
:type a: str
:type b: str
:rtype: int
"""
# check for invalid inputs
if (len(a) < 1) | (len(b) < 1) | (len(a) > 1e4) | (len(b) > 1e4):
return -1
# minimum repetitions of a for b to be a substring
minRep = int(len(b)/len(a))
rep = minRep
# increment the number of repetitions at most twice (as in the worst case below)
# b b b b b b b b
# a a a-a a a-a a a-a a a
while (rep <= min( minRep + 2, 1e4 )) and not (b in a*(rep)):
rep += 1
# The cycle exits when b is a substring of a repeated or if all the possible concatenations have been already checked
# In the second case, the output is set to -1
if not (b in a*(rep)):
rep = -1
return rep