686. 重复叠加字符串匹配

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 区间范围内。

LeetCode686. Repeated String Match

Java 实现

class Solution {
public int repeatedStringMatch(String A, String B) {
int m = A.length(), n = B.length();
StringBuilder str = new StringBuilder();
for (int i = 1; i <= n / m + 2; i++) {
str.append(A);
if (str.indexOf(B) != -1) {
return i;
}
}
return -1;
}
}
class Solution {
public int repeatedStringMatch(String A, String B) {
int count = 0;
StringBuilder str = new StringBuilder();
while (str.length() < B.length()) {
str.append(A);
count++;
}
if (str.toString().contains(B)) {
return count;
}
if (str.append(A).toString().contains(B)) {
return ++count;
}
return -1;
}
}

相似题目

参考资料

最新文章

  1. c 小工具的使用
  2. MongoDB安全和认证
  3. css 修改滚动条
  4. typedef关键字
  5. 读书笔记——Windows环境下32位汇编语言程序设计(9)ANSII字符大小写转大写
  6. test generation和MBIST
  7. 剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符
  8. Makefile学习与进阶之Makefile.am和$$(M)的意思
  9. Android KitKat 4.4 Wifi移植之Wifi driver
  10. UVA 1546 - Complete the sequence!(差分法)
  11. EF 关系描述
  12. jQuery扩展easyui.datagrid,添加数据loading遮罩效果代码
  13. VS中Ctrl+F5(开始执行不调试)一闪而过问题
  14. Netty4.x整合SpringBoot2.x使用Protobuf3详解
  15. django的闪现和增、删、改、查
  16. C# WebApi过滤器(开发接口必备利器)
  17. 050 sqoop的使用
  18. HashSet, HashTable
  19. Singer 学习十 同步模式
  20. H5做的商城客户端,效果很不错

热门文章

  1. Java 锁(学习笔记)
  2. 编程用泰勒公式求e的近似值,直到最后一项小于10的负6次方为止。
  3. WinDbg常用命令系列---内存查看d*
  4. for循环计算
  5. ubuntu 基于windows
  6. 洛谷 P2947 [USACO09MAR]向右看齐Look Up
  7. Python题库系列分享一(17道)
  8. vue Uncaught SyntaxError: Unexpected token &lt; 报错
  9. 当变量超过任意设定的变量限制时终止fluent模拟【翻译】
  10. 剑指offer:二叉搜索树与双向链表