[抄题]:

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").

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

  1. 不知道sb类的.append() .contains()方法,而且转换成字符串时还要.tostring()

[一句话思路]:首先保证达到长度相等的门槛再说,这不难想但也是第一次见

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 加到》B的长度就行了,不用加几千几万次了,因为后面都是重复的:第一次见
  2. 默认返回时是-1时要先写

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

sb类的append方法独出一帜

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int repeatedStringMatch(String A, String B) {
//cc
if (B.length() == 0) {
return -1;
}
//ini to the same length
int count = 0;
StringBuilder sb = new StringBuilder();
while (sb.length() < B.length()) {
sb.append(A);
count++;
}
//enough or not
if (sb.toString().contains(B)) {
return count;
}
if (sb.append(A).toString().contains(B)) {
return ++count;
}
//return -1
return -1;
}
}

686. Repeated String Match 字符串重复后的子字符串查找的更多相关文章

  1. 【Leetcode_easy】686. Repeated String Match

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

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

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

  3. 【刷题笔记】686. Repeated String Match

    题意 题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A? 暴力解法 直接 A+A+...,到哪次 A 包含 B 了,就返 ...

  4. 686. Repeated String Match判断字符串重复几次可以包含另外一个

    public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...

  5. [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 ...

  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. 686. Repeated String Match

    方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...

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

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

  9. 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 ...

随机推荐

  1. I.MX6 U-Boot ping网络

    /********************************************************************* * I.MX6 U-Boot ping网络 * 说明: * ...

  2. (三)canvas绘制样式

    beginPath() 对画线点的一个开始限制 moveTo() 画线的起点,只在开头使用 参数两个x轴,y轴 lineTo() 后续连线 两个参数x轴,y轴 stroke() 连线无填充 fill( ...

  3. php 中的杂项函数

    1.$arr = range(1, 10);   print_r($arr); Array(    [0] => 1    [1] => 2    [2] => 3    [3] = ...

  4. ubuntu下访问支付宝官网,安装安全控件

    (1)根据支付宝提示下载安装控件的压缩包 aliedit.tar.gz (2)解压安装 (3)重启浏览器就可以了

  5. SEO优化---10分钟学会建立高转化率的网站关键词库

    想要优化好一个网站,行业的分析,以及关键词的挖掘是必要的,有一定的关键词排名了,但是转化率和流量方面却很不理想这种情况大部分是只注重了有指数的关键词排名,而忽略了长尾关键词和一些没有指数但是可以带来巨 ...

  6. RAC修改数据库的spfile位置

    RAC修改spfile位置 [root@rac1 ~]# su - oracle [oracle@rac1 ~]$ sqlplus  / as sysdba SQL*Plus: Release 11. ...

  7. bzoj 4104 [Thu Summer Camp 2015]解密运算——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4104 想了很久,想出一个 nlogn (也许是 n2logn )的,可惜空间是 n2 . 已 ...

  8. erlang分布式例子

    抄袭自 http://www.blogjava.net/killme2008/archive/2007/06/29/127099.html 简单的说,就是 主机上需要同时启用短节点名,或者长节点名 保 ...

  9. strtotime出现时区问题不一致的解决方法

    学习源头:https://blog.csdn.net/longjuanfengzc/article/details/80622842 https://segmentfault.com/q/101000 ...

  10. 转:Oracle里几组重要的视图--v$sysstat,v$system_event,v$parameter v$system_parameter

    按组分的几组重要的性能视图 1.System 的 over view v$sysstat , v$system_event  , v$parameter,V$instance得到oracle_sid ...