-
Notifications
You must be signed in to change notification settings - Fork 8
/
solution.py
43 lines (41 loc) · 1.06 KB
/
solution.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
34
35
36
37
38
39
40
41
42
43
"""
57 / 57 test cases passed.
Runtime: 40 ms
Memory Usage: 15.1 MB
"""
from itertools import reduce
import operator
class Solution:
def repeatedStringMatch(self, a: str, b: str) -> int:
a_bmp = reduce(operator.or_, map(lambda x: 1 << ord(x), list(a)), 0)
b_bmp = reduce(operator.or_, map(lambda x: 1 << ord(x), list(b)), 0)
if a_bmp & b_bmp != b_bmp:
return -1
times = (len(b) - 1) // len(a) + 1
temp = a * times
if b in temp:
return times
temp += a
if b in temp:
return times + 1
else:
return -1
"""
57 / 57 test cases passed.
Runtime: 24 ms
Memory Usage: 15 MB
"""
class Solution2:
def repeatedStringMatch(self, a: str, b: str) -> int:
a_set, b_set = set(a), set(b)
if a_set & b_set != b_set:
return -1
times = (len(b) - 1) // len(a) + 1
temp = a * times
if b in temp:
return times
temp += a
if b in temp:
return times + 1
else:
return -1