Skip to content

Latest commit

 

History

History
43 lines (27 loc) · 1.28 KB

686-repeated-string-match.md

File metadata and controls

43 lines (27 loc) · 1.28 KB

686. Repeated String Match - 重复叠加字符串匹配

给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1。

举个例子,A = "abcd",B = "cdabcdab"。

答案为 3, 因为 A 重复叠加三遍后为 “abcdabcdabcd”,此时 B 是其子串;A 重复叠加两遍后为"abcdabcd",B 并不是其子串。

注意:

 A 与 B 字符串的长度在1和10000区间范围内。


题目标签:String

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
python3 360 ms N/A
class Solution:
    def repeatedStringMatch(self, A, B):
        """
        :type A: str
        :type B: str
        :rtype: int
        """
        for i in range(1, len(B) // len(A) + 4):
            if B in A*i:
                return i
        return -1