原题链接在这里:https://leetcode.com/problems/repeated-string-match/description/

题目:

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.

题解:

A重复append自己知道比B长,看B是否包含在内,若包含返回当前count.

若没有再append次A. 若B能是substring, 这个长度已经可以包含所有可能性. B的开头在0到A.length()-1的任何位置都足够盖住B.

Time Complexity: O(A.length()+B.length()). create个长度为A.length()+B.length()的string. 再用B找index.

Space: O(A.length()+B.length()).

AC Java:

class Solution {
public int repeatedStringMatch(String A, String B) {
int count = 0;
StringBuilder sb = new StringBuilder();
while(sb.length() < B.length()){
sb.append(A);
count++;
} if(sb.indexOf(B) >= 0){
return count;
}else if(sb.append(A).indexOf(B) >= 0){
return count+1;
}else{
return -1;
}
}
}

LeetCode Repeated String Match的更多相关文章

  1. [LeetCode] 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 su ...

  2. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. LeetCode 686. 重复叠加字符串匹配(Repeated String Match)

    686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...

  4. LeetCode686——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 ...

  5. 【Leetcode_easy】686. Repeated String Match

    problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...

  6. [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 su ...

  7. LeetCode算法题-Repeated String Match(Java实现)

    这是悦乐书的第289次更新,第307篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第156题(顺位题号是686).给定两个字符串A和B,找到A必须重复的最小次数,使得B是 ...

  8. 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 su ...

  9. C#LeetCode刷题之#686-重复叠加字符串匹配(Repeated String Match)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3963 访问. 给定两个字符串 A 和 B, 寻找重复叠加字符串A ...

随机推荐

  1. 关于IDEA导出项目jar包/runnable jar

    将项目导出为jar包分为 runnable jar 与 普通jar包 一.导出为普通jar包 该jar包中只有项目源代码, java -cp wordcount.jar 用来运行普通jar包 1.打开 ...

  2. ==、equals与hashCode

    ==  首先,得说明java数据类型分为基本数据类型和引用数据类型, 基本数据类型有8种: 浮点型:float(4 byte), double(8 byte) 整型:byte(1 byte), sho ...

  3. 继承Thread类与实现Runnable接口

    java中创建线程有两种方式: 1. 类继承Thread类,重写run方法,每创建一个实例对象即开启一个线程 2. 类实现Runnable接口,重写run方法,将实例对象传入新建Thread的方法: ...

  4. matplotlib模块之plot画图

    关于matplotlib中一些常见的函数,https://www.cnblogs.com/TensorSense/p/6802280.html这篇文章讲的比较清楚了,https://blog.csdn ...

  5. JSP语法及内置对象

    JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它[1]  是由Sun Microsystems公司倡导.许多公司参与一起建立的一种动 ...

  6. 在Visual Studio中使用VueJS时,不可以用 v-bind 的简写 : 及 v-on的简写 @

    在Visual Studio中使用VueJS时,不可以用 v-bind 的简写 : 及 v-on的简写 @ 一方面 @符号和 Razor引擎冲突, 另外,当使用VS的格式化代码功能时, 会把 html ...

  7. 【P2016】战略游戏(贪心||树状DP)

    这个题真是...看了一会之后,发现有一丝丝的熟悉,再仔细看了看,R,这不是那个将军令么...然后果断调出来那个题,还真是,而且貌似还是简化版的...于是就直接改了改建树和输入输出直接交了..阿勒,就2 ...

  8. java hasmap对象的深复制实现:字节码复制和对象序列化成字符串复制比较。

    /** * Created by Administrator on 2016/11/23. */ public class test { public static void main(String[ ...

  9. Debugging Tools for Windows__from WDK7

    1. 主要要用到两个工具: (1).WinDBG 这个主要用于 非IDE下 调试程序/查看信息等 (2).cdb.exe 这个主要是用在 Qt5.3.2 for VS10 的单步调试器 2. WDK7 ...

  10. cms实例笔记(二)

    栏目分级: 一.首页 二.解决方案 (栏目) 1.栏目模型 名称: 新闻: 封面模板:cover.html (没有子栏目模型不会用到) 列表页模板:list.html 2.文档模型 名称:新闻 模板: ...