Leetcode686.Repeated String Match重复叠加字符串匹配
给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1。
举个例子,A = "abcd",B = "cdabcdab"。
答案为 3, 因为 A 重复叠加三遍后为 “abcdabcdabcd”,此时 B 是其子串;A 重复叠加两遍后为"abcdabcd",B 并不是其子串。
注意:
A 与 B 字符串的长度在1和10000区间范围内。
为什么(len2 / len1 + 2) * len1中要加2
比如字符串A: "abcd"
字符串B如果有答案
无非有三种情况
1.
"abcd" = len2 / len1
2.
"..cd"(不等于A,相当于A的后缀) + "abcd" * n = len2 / len1 + 1
或
"abcd" * n + "ab.."(不等于A,相当于A的前缀) = len2 / len1 + 1
3.
"..cd" + "abcd" * n + ".ab.." = len2 / len1 + 2
class Solution {
public:
int repeatedStringMatch(string A, string B) {
string temp = A;
int cnt = 1;
int len1 = A.size();
int len2 = B.size();
int boundary = (len2 / len1 + 2) * len1;
for(int i = len1; i <= boundary; i += len1)
{
string:: size_type idx;
idx = A.find(B);
if(idx != string::npos)
{
return cnt;
}
A += temp;
cnt++;
}
return -1;
}
};
Leetcode686.Repeated String Match重复叠加字符串匹配的更多相关文章
- LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...
- Leetcode 686.重复叠加字符串匹配
重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd", ...
- Java实现 LeetCode 686 重复叠加字符串匹配
686. 重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd&q ...
- 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 ...
- [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 ...
- Q686 重复叠加字符串匹配
给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = " ...
- [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 重复字符串匹配
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 ...
随机推荐
- Linux网桥端口的arp问题
Linux的brctl addif命令可以将一个接口加入到既有的网桥中,接下来,这个接口就成了brport,属于一个从属的接口,然而你还是可以看到它的,并且可以为它添加IP地址,然后route命令会显 ...
- sql不用拼接语句实现动态查询条件
DECLARE @oFrom INT SELECT * FROM baseinfo AND ( ( and Type = 'Breakfast') ) or的条件可自由添加,尤其适用互斥条件的查询.
- Windows的进程间通信
对于任何一个现代的操作系统,进程间通信都是其系统结构的一个重要组成部分.而说到Windows的进程(线程)间通信,那就要看是在什么意义上说了.因为正如"Windows的跨进程操作" ...
- SPSS超详细操作:分层回归(hierarchical multiple regression)
SPSS超详细操作:分层回归(hierarchical multiple regression) 1.问题与数据 最大携氧能力(maximal aerobic capacity, VO2max)是评价 ...
- 05-1-操作css样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ES6之字符串学习
以下是常用的方法不是全部方法 1.codePointAt()方法 有一些字段需要4个字节储存,这样charCodeAt方法的返回就是不正确的,用codePointAt()方法就可以返回 十进制的值.如 ...
- net.sf.json JSONObject与JSONArray使用实例
实例自己想的一个实例应用场景:一个人可以有多个角色,例如:在家中是儿子,在学校是学生,在公司是程序员,一个人还可以办好多业务 * 每个业务好多个人都可以办,则标记(mark)就是记录这唯一标识的(如i ...
- 嘴巴题5 「BZOJ1864」[ZJOI2006] 三色二叉树
1864: [Zjoi2006]三色二叉树 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1195 Solved: 882 [Submit][Status ...
- 关于Canvas的坐标系
注意Canvas的坐标系应该是这样子的: 看下面的例子: 最后的显示效果是:
- 廖雪峰Java10加密与安全-4加密算法-1对称加密算法
1.对称加密算法 加密和解密使用同一个密钥,例如WinRAR. WinRAR在对文件进行打包的时候,可以设置一个密码,在解压的时候需要使用同样的密码才能正确的解压. 加密:encrypt(key,me ...