给定两个字符串 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重复叠加字符串匹配的更多相关文章

  1. LeetCode 686. 重复叠加字符串匹配(Repeated String Match)

    686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...

  2. Leetcode 686.重复叠加字符串匹配

    重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd", ...

  3. Java实现 LeetCode 686 重复叠加字符串匹配

    686. 重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd&q ...

  4. 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 ...

  5. [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 ...

  6. Q686 重复叠加字符串匹配

    给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = " ...

  7. [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 ...

  8. [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 ...

  9. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. linux ssh密钥认证, 免密码登陆

    1. 客户端生成密钥 # mkdir ~/.ssh # chmod ~/.ssh # cd ~/.ssh 生成RSA密钥 # ssh-keygen -t rsa (然后连续三次回车) 2. 把公钥传到 ...

  2. 菜鸟nginx源码剖析数据结构篇(十一) 共享内存ngx_shm_t[转]

    菜鸟nginx源码剖析数据结构篇(十一) 共享内存ngx_shm_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn ...

  3. 《DSP using MATLAB》Problem 8.25

    用match-z方法,将模拟低通转换为数字低通 代码: %% --------------------------------------------------------------------- ...

  4. MyBatis - sqlMapConfig.xml主配置文件

    SqlMapConfig.xml配置文件的内容和配置顺序如下 ① properties(读取配置文件):定义配置,配置的属性可以在整个配置文件中其他位置进行引用: ② settings(全局配置参数) ...

  5. Python实例1-Collatz 序列

    编写一个名为 collatz()的函数,它有一个名为 number 的参数.如果参数是偶数,那么 collatz()就打印出 number // 2, 并返回该值.如果 number 是奇数, col ...

  6. PKUOJ 区间内的真素数

    http://bailian.openjudge.cn/tm2018/A/ #include <iostream> #include <math.h> #include < ...

  7. 根据url的属性名来取属性值赋值给js

    1.方法一:js的正则表达式:请求路径:http://127.0.0.1/pec/jsp/member/refundOrder.jsp?status=4 <script> var stat ...

  8. centos Python2.6 升级到2.7

    需求: centos 6.x 系统默认Python 版本都是2.6,实际生产环境中需要用到Python 2.7.x Python 2.7 下载地址 [root@ansible package]# wg ...

  9. 软件-MQ-MQ:IBM MQ

    ylbtech-软件-MQ-MQ:MQ(IBM MQ) MQ传递主干,在世界屡获殊荣. 它帮您搭建企业服务总线(ESB)的基础传输层.IBM WebSphere MQ为SOA提供可靠的消息传递.它为经 ...

  10. Boost test vs2013 fatal error C1001

    Boost test vs2013 fatal error C1001 Boost test库提供了一个用于单元测试的基于命令行界面的测试套件UTF:Unit Test Framework,具有单元测 ...