原题链接在这里: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. Loadrunder脚本篇——Run-time Settings之Browser Enmulation

    浏览器模拟 所有Internet Vuser Header包含一个标识将被模拟的浏览器类型(或无线工具包)的User Agent header.例如User-Agent: Mozilla/3.01Go ...

  2. Loadrunder之脚本篇——关联函数对话框详解

    Insert->New Step,打开Add Step对话框 选择函数web_reg_save_param,点击OK,打开关联函数设置窗口 说明: Parameter Name 此处设置存放参数 ...

  3. iOS 什么是函数式编程

    前言:当前只做理解性的常规背书,根据不断深入学习会不断丰富解读内容,欢迎评论提意见 函数式编程:Functional Programming 1 基本解释: 函数式编程 是一种思维模式,一种编程思想, ...

  4. Ajax在jQuery中的应用---加载异步数据

    Ajax是Asynchronous JavaScript and XML的缩写,其核心是通过XMLHttpRequest对象,以一种异步的方式,向服务器发送数据请求,并通过该对象接收请求返回的数据,从 ...

  5. openGL学习进程(1)OpenGL的简介

        通过本节,我们来简要了解一下openGL.     (1)OpenGL概述: openGL(Open Graphics Library)是个专业的图形程序接口,定义了一个跨编程语言.跨平台的编 ...

  6. python中编写无参数decorator

    Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法,这样可以避免手动编写 f = de ...

  7. Python编程-多态、封装、特性

    一.多态与多态性 1.多态 (1)什么是多态 多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的概念依赖于继承) 序列类型有多种形态:字符串,列表,元组. 动物有多种形态:人,狗,猪 文 ...

  8. cocos2dx打飞机项目笔记一:项目结构介绍

    最近在学习cocos2dx引擎,版本是2.1.3,开发环境是win7 + vs2010,模仿微信打飞机游戏,开发中参考了 csdn 偶尔e网事 的系列文章:http://blog.csdn.net/c ...

  9. 数据结构与算法分析:C语言描述(原书第2版 简体中文版!!!) PDF+源代码+习题答案

    转自:http://www.linuxidc.com/Linux/2014-04/99735.htm 数据结构与算法分析:C语言描述(原书第2版中文版!!!) PDF+源代码+习题答案 数据结构与算法 ...

  10. EXP-00008:遇到ORACLE错误904问题

    案例情景--在一次Oracle 数据库导出时: C:\Documents and Settings\Administrator>exp lsxy/lsxy@lsxy_db file=E:\lsx ...