# 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 条评论

## 相关文章

### 网上Silverlight项目收集

1、Silverlight + OpenXML Video  ：This is a Silverlight proof of concept applicati...

1959

### 文档宽高及窗口事件

onscroll：当滚动条滚动的时候触发 onresize：当窗口大小发生改变的时候触发

782

2984

2246

732

### Canvas 给图形绘制阴影

/** * 图形绘制阴影 */ function initDemo6() { var canvas = document.getElementByI...

3577

622

1093

### Silverlight 2 和 Deep Zoom

Steve Clayton 发布了一篇关于 Silverlight 2 演示的文章，其中有提到一项叫作 Deep Zoom 的技术，现部署在 http://me...

1776

### cocos2dx 2048

#include "GameScene.h" #include "CardSprite.h" #include "ScoreBoard.h" #include ...

3648