LeetCode Repeated String Match
原题链接在这里: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的更多相关文章
- [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 ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 【Leetcode_easy】686. Repeated String Match
problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...
- [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算法题-Repeated String Match(Java实现)
这是悦乐书的第289次更新,第307篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第156题(顺位题号是686).给定两个字符串A和B,找到A必须重复的最小次数,使得B是 ...
- 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 ...
- C#LeetCode刷题之#686-重复叠加字符串匹配(Repeated String Match)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3963 访问. 给定两个字符串 A 和 B, 寻找重复叠加字符串A ...
随机推荐
- Bootstrap主题库
主题 https://startbootstrap.com/template-categories/all/ https://bootstrapmade.com/ http://www.jqueryf ...
- 025_MapReduce样例Hadoop TopKey算法
1.需求说明
- 一些逼格略高的 js 片段
// 一个接一个运行 // timeout 不能直接放在 for 里面,暂时不知道为什么 function functionOneByOne(fn, times, duration) { for(va ...
- usb2.0 协议分析
转:https://blog.csdn.net/u011594613/article/details/48291307 一.USB硬件介绍1.1.概述 一条USB传输线分别由地线.电源线.D+和D-四 ...
- SOA、SOAP、REST、RPC
1.SOA SOA(面向服务的软件架构.Service Oriented Architecture),是一种软件设计模式,主要应用于不同应用组件之间通过某种协议来互操作.例如典型的 通信网络协议.因 ...
- PHP面向对象之对象和引用
在PHP中对象类型和简单变量类型表现可以说是大相径庭,很多数据类型都要可以在写时进行复制,如当写代码$a=$b时,两个变量因为赋予相同的值而告终.所以需要注意的是,这种情况用在对象时就会完全不同了. ...
- 安装MySQL5.7.18遇到的坑
最近才注意到MySQL的各个版本之间差别还挺大的,比如5.5.x版本的timestamp类型列只能有一个设置为default CURRENT_TIMESTAMP的,于是尝试了换成一个新版本是mysql ...
- Java注解处理器
Java注解处理器 2015/03/03 | 分类: 基础技术 | 0 条评论 | 标签: 注解 分享到:1 译文出处: race604.com 原文出处:Hannes Dorfmann Java ...
- 什么是CDN 域名预热
通常大家理解的预热功能是,缓存预热.将需要预热的资源推送到CDN边缘节点,用户访问时将不需要再回源站,可以直接命中缓存.这样可以加快资源的首次访问,同时缓解突增回源流量给源站造成的压力.这里说的是域名 ...
- mysql在表的某一位置增加一列的命令
如果想在一个已经建好的表中添加一列,可以用诸如: alter table t1 add column addr varchar(20) not null; 这条语句会向已有的表t1中加入一列addr, ...