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 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...
随机推荐
- 【转】.NET Core 事件总线,分布式事务解决方案:CAP
[转].NET Core 事件总线,分布式事务解决方案:CAP 背景 相信前面几篇关于微服务的文章也介绍了那么多了,在构建微服务的过程中确实需要这么一个东西,即便不是在构建微服务,那么在构建分布式应用 ...
- php+tcpdf如何把生成的pdf文件保存在服务端
tcpdf组件目前应用得非常广泛,但是对于如何把生成的pdf文件自动保存在服务端却很少有人提及.让我们先来看看标准输出代码: //服务器存档模式 $pdf->Output('output.p ...
- LeetCode 1043. Partition Array for Maximum Sum
原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...
- VSFTPD匿名用户上传文件
1.安装vsftpd yum -y install vsftpd yum -y install ftp 客户端 2.编写配置文件 vim /etc/vsftpd/vsftpd.conf anonymo ...
- 实训作业6 (数据I/O)
1. 文件输出流的应用. 定义如下字符串: String str = “12345abcdef@#%&*软件工程”; 编写程序将该字符串写入文件”data.txt”. import java. ...
- LOJ P10015 扩散 题解
每日一题 day49 打卡 Analysis 用dis数组记录每两个点之间的时间,再用一个传递闭包来维护最小的时间就好了 #include<iostream> #include<cs ...
- B/S大附件上传,支持断点续传
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- Visual C++ 里的 Classes, Methods and RTTI
类的基本布局 为了说明以下内容,让我们考虑这个简单的例子: class A { int a1; public: virtual int A_virt1(); virtual int A_virt2() ...
- 微信小程序前端function封装
funtion的封装 utils =>http.js var tips = { 1: "没有网络", 999: "无效的请求", 5000: " ...
- lower_bound( )和upper_bound( )怎么用嘞↓↓↓
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...