给定两个字符串 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. python 中动态类的创建

    参考 collections.namedtuple 的实现 链接: https://www.cnblogs.com/BeautifulWorld/p/11647198.html

  2. [COCI2019] Mobitel

    题目 显然不小于\(n\)这个东西我们不是很好搞,考虑正难则反,求出有多少条路径小于\(n\),之后拿\(C_{n+m}^m\)一减就好了 于是状态为\(dp[i][j][k]\)表示到\((i,j) ...

  3. vue表格之@row-click="handleSelect" 与setCurrentRow

    作用:表格行点击触发的事件 注意与@change.@selection-change事件的区分 <el-table ref="RoomTable" @row-click=&q ...

  4. C#跨域

    //在ConfigureServices中配置 #region 跨域 var urls = "*";//Configuration["AppConfig:Cores&qu ...

  5. php 7.2下mcrypt扩展支持附带的问题

    按照网上提供的mcrypt扩展编译支持方法,完成了扩展编译,也确实可以正常加密/解密了 但是如果php.ini中配置为: error_reporting = E_ALL display_errors ...

  6. Nginx的几个重要模块

    ngx_http_ssl_module 让Nginx可以支持HTTPS的模块,此模块下的大多数指令都应用在http,server上下文 ①ssl on | off; 是否开启ssl功能 ②ssl_ce ...

  7. deepfake安装python常用命令

    pip install -r requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ python -m p ...

  8. HZOI20190902模拟35题解

    题面: A:公园 DAG上想拓扑dp 然而博主记忆化搜索了一下 设f[i][j]表示从i节点走j个点出公园所用的最小时间 则$f[u][i]=min(f[v][j-1]+dis_{u,v})$; 然后 ...

  9. day21 作业

    1.定义MySQL类 1.对象有id.host.port三个属性 2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一 3.提供两种实例化方式,方式一:用户传入host和po ...

  10. Yaf--个人封装yaf的框架+swoole+elasticsearch(Window+linux版)

    这是基于c写底层的yaf框架集成PDO+predis+读写分离+composer+全局异常处理+多模块开发+Log日志记录简单容易上手的框架 注意:window版没有swoole和Smarty主要用作 ...