[
string
kmp
]
Leetcode 0686 Repeated String Match
Problem statement
https://leetcode.com/problems/repeated-string-match/
Solution
Let k = len(b)//len(a)
, then it can be shown that answer can be either k
, k + 1
or k + 2
.
Complexity
If we assume that in
working in linear time, complexity will be O(m + n)
.
Code
class Solution:
def repeatedStringMatch(self, a, b):
k = len(b)//len(a)
if b in a*k: return k
if b in a*(k+1): return k+1
if b in a*(k+2): return k+2
return -1