LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配
686. Repeated String Match
题目描述
给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的子串,如果不存在则返回 -1。
举个例子,A = "abcd",B = "cdabcdab"。
答案为 3,因为 A 重复叠加三遍后为 "abcdabcdabcd",此时 B 是其子串;A 重复叠加两遍后为 "abcdabcd",B 并不是其子串。
注意:
A 与 B 字符串的长度在 1 和 10000 区间范围内。
LeetCode686. Repeated String Match
Java 实现
class Solution {
public int repeatedStringMatch(String A, String B) {
int m = A.length(), n = B.length();
StringBuilder str = new StringBuilder();
for (int i = 1; i <= n / m + 2; i++) {
str.append(A);
if (str.indexOf(B) != -1) {
return i;
}
}
return -1;
}
}
class Solution {
public int repeatedStringMatch(String A, String B) {
int count = 0;
StringBuilder str = new StringBuilder();
while (str.length() < B.length()) {
str.append(A);
count++;
}
if (str.toString().contains(B)) {
return count;
}
if (str.append(A).toString().contains(B)) {
return ++count;
}
return -1;
}
}
相似题目
参考资料
- https://leetcode.com/problems/repeated-string-match/
- https://leetcode-cn.com/problems/repeated-string-match/
LeetCode 686. 重复叠加字符串匹配(Repeated String Match)的更多相关文章
- [Swift]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 su ...
- Java实现 LeetCode 686 重复叠加字符串匹配
686. 重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd&q ...
- Leetcode 686.重复叠加字符串匹配
重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd", ...
- Leetcode686.Repeated String Match重复叠加字符串匹配
给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = " ...
- LeetCode 942. 增减字符串匹配(DI String Match) 49
942. 增减字符串匹配 942. DI String Match 题目描述 每日一算法2019/6/21Day 49LeetCode942. DI String Match Java 实现 and ...
- Q686 重复叠加字符串匹配
给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = " ...
- LeetCode.942-DI字符串匹配(DI String Match)
这是悦乐书的第361次更新,第388篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第223题(顺位题号是942).给定仅包含I(增加)或D(减少)的字符串S,令N = S ...
- [Swift]LeetCode942. 增减字符串匹配 | DI String Match
Given a string S that only contains "I" (increase) or "D" (decrease), let N = S. ...
- LeetCode 459. 重复的子字符串(Repeated Substring Pattern)
459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...
随机推荐
- janusgraph-图数据库的学习(1)
图数据库的简介-来源百度百科 1.简介 图形数据库是NoSQL数据库的一种类型,它应用图形理论存储实体之间的关系信息.图形数据库是一种非关系型数据库,它应用图形理论存储实体之间的关系信息.最常见例子就 ...
- Educational Codeforces Round 67
Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...
- (转)React事件处理函数必须使用bind(this)的原因
1.JavaScript自身特性说明如果传递一个函数名给一个变量,之后通过函数名()的方式进行调用,在方法内部如果使用this则this的指向会丢失.示例代码:首先我们创建test对象并直接调用方法 ...
- 02-线性结构4 Pop Sequence (25 分)
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- GoCN每日新闻(2019-10-21)
GoCN每日新闻(2019-10-21) GoCN每日新闻(2019-10-21) 1. 使用 Golang, RabbitMQ 和 Protobuf 构建高效的微服务 https://mediu ...
- GoCN每日新闻(2019-09-30)
GoCN每日新闻(2019-09-30) 1. 使用Sqlmock测试数据库 https://medium.com/ralali-engineering/testing-database-using- ...
- MAKEFILE编写学习--1
makefile是在编译中大型程序中使用的自动化编译工具make依赖的指令文件.这样可以使得程序的编译更加便捷快速. makefile的一般规则如下: target ... : prerequisit ...
- MSSQL手工注入 报错注入方法
例子:www.kfgtfcj.gov.cn/lzygg/Zixun_show.aspx?id=1[1]首先爆版本:http://www.kfgtfcj.gov.cn/lzygg/Zixun_show. ...
- JVM内存的划分
JVM内存的划分有五片: 1. 寄存器: 2. 本地方法区: 3. 方法区: 4. 栈内存: 5. 堆内存.
- IDEA实现SpringBoot热部署
1.pom.xml添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...