给定两个字符串 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. VS2010-MFC(对话框:为控件添加消息处理函数)

    转自:http://www.jizhuomi.com/software/156.html MFC为对话框和控件等定义了诸多消息,我们对它们操作时会触发消息,这些消息最终由消息处理函数处理.比如我们点击 ...

  2. #、%和$符号在OGNL表达式中的作用

    #.%和$符号在OGNL表达式中经常出现,而这三种符号也是开发者不容易掌握和理解的部分.在这里笔者简单介绍它们的相应用途. 1.#符号的用途一般有三种.   1)访问非根对象属性,例如示例中的#ses ...

  3. 简单数论 | Day3 部分题解

    A - The Euler function 来源:HDU 2824 计算[a,b]区间内的整数的欧拉函数值,需要掌握单个欧拉函数和函数表的使用. #include <iostream> ...

  4. @RestControllerAdvice作用及原理

    原文:Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理 在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@Exceptio ...

  5. Frame用navigate导航到新页面后导航条隐藏的方法

    设置Frame的NavigationUIVisibility="Hidden"

  6. windows server 文件夹和搜索选项被禁用了

    当我们需要调整 windows 文件夹相关的配置时,却发现“文件夹和搜索选项”被禁用了,下图是恢复正常的情况.被禁用时显示灰色,不能点击. 下面给出解决步骤: 打开“组策略”. 然后依次展开“用户配置 ...

  7. Java发送http请求发送json对象

    直接上代码: http工具类: public static String httpPostWithjson(String url, String json) throws IOException { ...

  8. 第一个入驻阿里云自营心选商城,如今它已经是营收过亿的SaaS独角兽

    淘宝心选.网易严选.小米有品...越来越多的企业电商自有品牌围绕“低价好物”大做文章,用创新赢得了市场.作为To B从业人员,不由思考:C端的成功是否可以复制在B端? 伴随着互联网下半场的到来,云计算 ...

  9. Laravel报错:1071 Specified key was too long; max key length is 1000 bytes

    Laravel安装,初始化数据库,运行下列命令时候出错 php artisan migrate 解决办法1,设置数据库引擎格式 //临时更改 SET GLOBAL default_storage_en ...

  10. PAT甲级——A1064 Complete Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...