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 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]:
[思维问题]:
- 不知道sb类的.append() .contains()方法,而且转换成字符串时还要.tostring()
[一句话思路]:首先保证达到长度相等的门槛再说,这不难想但也是第一次见
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 加到》B的长度就行了,不用加几千几万次了,因为后面都是重复的:第一次见
- 默认返回时是-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 字符串重复后的子字符串查找的更多相关文章
- 【Leetcode_easy】686. Repeated String Match
problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【刷题笔记】686. Repeated String Match
题意 题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A? 暴力解法 直接 A+A+...,到哪次 A 包含 B 了,就返 ...
- 686. Repeated String Match判断字符串重复几次可以包含另外一个
public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...
- [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 ...
- 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 ...
- 686. Repeated String Match
方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...
- LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...
- 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 ...
随机推荐
- 运行python代码
IPython IPython 'magic' function documentation
- 使用IntelliJ IDEA开发SpringMVC网站的学习
最近开始了“使用IntelliJ IDEA开发SpringMVC网站”的学习,有幸看到一份非常完善的学习资料,笔者非常用心的详细注释了一份关于博客的开发过程和细节,并且在评论中回复大家提出的问题,非常 ...
- openfaas 简单试用
1. 安装 faas-cli 参考以前文章,或者使用官方的shell脚本 2. 简单例子 mkdir rong cd rong faas-cli new rong --lang python / ...
- J-Link在SWD模式与MCU能连接成功但不能读写
今天在J-Link的排线末端引出3.3v.SWDIO.SWCLK.GND,连接到stm32上,发现只能连接成功,不能读和写,出现下面错误: - ERROR: RAM check failed @ ad ...
- 2、通过HBase API进行开发
一.将HBase的jar包及hbase-site.xml添加到IDE 1.到安装HBase集群的任意一台机器上找到HBase的安装目录,到lib目录下下载HBase需要的jar包,然后再到conf目录 ...
- Does Windows have a limit of 2000 threads per process?
http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx Often I see people asking why the ...
- 【转】WINSOCKET客户端编程以及JMETER外部调用
1 public class SocketClient { 2 OutputStream clientout = null; 3 InputStream clienIn = null; 4 byte[ ...
- java继承实例
题目:1./*定义一个Person类,这个类的属性有:name.age.color类有构造方法给3个属性赋值类有run方法,能计算出十年后的年龄并输出.类有eat方法,能改变自己的name和color ...
- 2012_p2 寻宝 (treasure.cpp/c/pas)
2012_p2 寻宝 (treasure.cpp/c/pas) 时间限制: 1 Sec 内存限制: 128 MB提交: 23 解决: 9[提交][状态][讨论版][命题人:外部导入] 题目描述 2 ...
- Java-Runoob:Java 变量类型
ylbtech-Java-Runoob:Java 变量类型 1.返回顶部 1. Java 变量类型 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifie ...