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 ...
随机推荐
- LEX下出毛病的问题
毛病! 1.今日写词法分析,回想起第一次写时候的蓝色警告:不要随便管理员.so,便Win+R,"cmd",回车. 2.在用lex写的时候,注意注释是 /*注释放于此处*/ 而非一般 ...
- (四)js数组方法一
ES5数组方法: Array.prototype.filter() 对数组元素进行过滤 三个参数:元素值,下标,原数组 返回:过滤后符合条件的数组,不会改变原数组 let arr = [2,4,6 ...
- 【ES6】箭头函数
let getPrices = () => 4.55 console.log(getPrices()) let arr = ['apple', 'banana', 'orange'] arr.f ...
- 数据结构之最小生成树Kruskal算法
1. 克鲁斯卡算法介绍 克鲁斯卡尔(Kruskal)算法,是用来求加权连通图的最小生成树的算法. 基本思想:按照权值从小到大的顺序选择n-1条边,并保证这n-1条边不构成回路. 具体做法:首先构造一个 ...
- Linux内核开发
Linux内核开发 https://www.cnblogs.com/pugang/p/9728983.html 很早之前就想写一篇文章总结一下Linux Kernel开发的相关资料,项目的原因,再加上 ...
- Android 比对APK的签名信息
https://www.jianshu.com/p/8583f6a966e2 在做App的时候经常会有验证apk是否为正版的需求,比如一些接入第三方支付的app,接入微信sdk也是需要apk签名信息的 ...
- git问题记录
1.从远程仓库拉取A文件,在本地删掉了这个A文件,然后再去拉取远程仓库,是拉不下来的,因为本地版本库比远程库的高 我觉得是第一次远程的版本已经拉取了,本地已记录,远程程没有发生变化,在拉取和先前的一致 ...
- expected_conditions 库的使用方法
from selenium.webdriver.support import expected_conditions as EC 例子一: 例子二:(判断元素存在文本"糯米")
- 【转】Jmeter 代理服务器录制脚本
因为Badboy只支持IE浏览器,现在又不得不到到fireFox上录制脚本才能完成,再次重温下Jmeter自带的Http代理服务器,当然比起badboy,它稍稍麻烦点. 1 配置很简单 工作台--& ...
- Spring XML和Annotation混合配置的时候,XML中Bean名称写错会导致启动异常不打印、死循环
今天做Tomcat迁移Spring Boot,遇到一个坑.启动没有错误,CPU特别高 经过把堆栈kill -3 打印出来,发现堆栈特别长(没有死循环),所有的堆栈信息都集中在org.springfra ...