# LWC 52：686. Repeated String Match

## LWC 52：686. Repeated String Match

Problem:

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 int repeatedStringMatch(String A, String B) {
int nb = B.length();
int na = A.length();
int times = nb / na + 2;
StringBuilder sb = new StringBuilder(A);
for (int i = 1; i <= times; ++i) {
if (sb.toString().contains(B)) return i;
else {
sb.append(A);
}
}
return -1;
}```

times的上界可以设置的大点，当然+2已经是最紧的上界了。

98 篇文章33 人订阅

0 条评论

## 相关文章

### 【编程题】Java编程题五（10道）

【编程题】Java编程题六（10道） 【程序41】 题目：海滩上有一堆桃子，五只猴子来分。第一只猴子把这堆桃子凭据分为五份，多了一个，这只猴子把多的一个扔入海...

43012

622

863

823

### JavaScript学习笔记002-数据类型0字符串拼接

[十位] [个位] [几何] [子集] [大圆] [小圆] [元素] [下标] [分子] [分母] [分数] [中点] [约分] [加数] [减数]

791

794

4322

### 3097: Hash Killer I

3097: Hash Killer I Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special Judge Su...

1994

571

### Ffmpeg对sps/pps的解析和格式转换

H264流的 SPS（序列参数集Sequence Parameter Set）和PPS（图像参数集Picture Parameter Set）记录了视频的基本编...

2192