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

@TOC

## # 题目描述

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.

For example, with A = "abcd" and B = "cdabcdab".

Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").

Note:

The length of A and B will be between 1 and 10000.

## # 解题方法

python的除会向下取整，所以多加几次，比如+3。（+2会错误）

``````class Solution(object):
def repeatedStringMatch(self, A, B):
"""
:type A: str
:type B: str
:rtype: int
"""
na, nb = len(A), len(B)
times = nb / na + 3
for i in range(1, times):
if B in A * i:
return i
return -1
``````

C++版本如下：

``````class Solution {
public:
int repeatedStringMatch(string A, string B) {
int NA = A.size(), NB = B.size();
int times = NB / NA + 3;
string t = A;
for (int i = 1; i < times; i++) {
if (t.find(B) != string::npos) return i;
t += A;
}
return -1;
}
};
``````

[LeetCode] Repeated String Match 重复字符串匹配open in new window

## # 日期

2018 年 3 月 15 日 --雾霾消散，春光明媚 2018 年 11 月 24 日 —— 周日开始！一周就过去了～