# C#LeetCode刷题之#686-重复叠加字符串匹配（Repeated String Match）

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.

```public class Program {

public static void Main(string[] args) {
var A = "abcd";
var B = "cdabcdab";

var res = RepeatedStringMatch(A, B);
Console.WriteLine(res);

Console.ReadKey();
}

private static int RepeatedStringMatch(string A, string B) {
var length = Math.Ceiling(B.Length / (double)A.Length) + 1;
var repeat = new StringBuilder();
for(var i = 0; i < length; i++) {
repeat.Append(A);
if(repeat.Length < B.Length) continue;
if(repeat.ToString().Contains(B)) return i + 1;
}
return -1;
}

}```

`3`