[抄题]:

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. 【retina】手机上 1PX 边框

    1像素边框,在2倍屏幕上为2“占位”,3倍屏上为3“占位”,但设计师就要1“占位” .content h1:after, .content h2:after { border-top: 1px sol ...

  2. 网络爬虫必备知识之requests库

    就库的范围,个人认为网络爬虫必备库知识包括urllib.requests.re.BeautifulSoup.concurrent.futures,接下来将结对requests库的使用方法进行总结 1. ...

  3. remoting与socket、web service的比较及实例

    remoting基础 一种分布式处理方式,可以说是DCOM的一种升级 跨过应用程序域,与另外的应用程序域进行通信,即穿越边界 在remoting中是通过通道(channel)来实现两个应用程序域之间对 ...

  4. 解决direct2d拖拽窗口闪烁

    响应WM_ERASEBKGND,在OnEraseBkgnd()处返回FALSE,阻止GDI重绘客户区背景色,设置背景色的工作交给Direct2D在Render时设置,否则在Resize时会出现窗口闪烁 ...

  5. .Net Remoting编程 ---- 系列文章

    .Net Remoting(应用程序域) - Part.1 摘要: 本文是.Net Remoting系列的第一篇文章,讲述了Remoting的“前驱知识点”--应用程序域.传值封送(Marshal b ...

  6. 软件开发中IT用语-日文和英文对照版

    開発工程 † 要件定義…Requirement Definition (Analyze) 外部設計…External Design 内部設計…Internal Design 開発…Coding テスト ...

  7. PostgreSQL备份

    备份与恢复 postgresql自带了两个备份工具: pg_dump:可备份一个指定的database pg_dumpall:可一次性备份所有database数据以及系统全局数据  使用pg_dump ...

  8. Hibernate问题集锦: 概述

    Hibernate问题集锦: 概述   ImportNew注: 本文是ImportNew编译整理的Java面试题系列文章之一.你可以从这里查看全部的Java面试系列. Q.怎么配置hibernate? ...

  9. delphi 理解ParamStr

    delphi 理解ParamStr 演示代码如下 ::code procedure TForm1.FormCreate(Sender: TObject); var   i: Integer; begi ...

  10. 在rebar发布的项目中添加监视工具

    默认使用rebar创建的项目没法使用observer,可以如下操作 修改app.src {application, tcp_server, [ {description, ""}, ...