Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.

For example, with A = "abcd" and B = "cdabcdab".

Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").

Note:
The length of A and B will be between 1 and 10000.

这道题让我们用字符串B来匹配字符串A,问字符串A需要重复几次,如果无法匹配,则返回-1。那么B要能成为A的字符串,那么A的长度肯定要大于等于B,所以当A的长度小于B的时候,我们可以先进行重复A,直到A的长度大于等于B,并且累计次数cnt。那么此时我们用find来找,看B是否存在A中,如果存在直接返回cnt。如果不存在,我们再加上一个A,再来找,这样可以处理这种情况A="abc", B="cab",如果此时还找不到,说明无法匹配,返回-1,参见代码如下:

解法一:

class Solution {
public:
int repeatedStringMatch(string A, string B) {
int n1 = A.size(), n2 = B.size(), cnt = ;
string t = A;
while (t.size() < n2) {
t += A;
++cnt;
}
if (t.find(B) != string::npos) return cnt;
t += A;
return (t.find(B) != string::npos) ? cnt + : -;
}
};

下面这种解法就更简洁了,思路和上面的一样,都是每次给t增加一个字符串A,我们其实可以直接算出最多需要增加的个数,即B的长度除以A的长度再加上2,当B小于A的时候,那么可能需要两个A,所以i就是小于等于2,这样我们每次在t中找B,如果找到了,就返回i,没找到,就增加一个A,循环推出后返回-1即可,参见代码如下:

解法二:

class Solution {
public:
int repeatedStringMatch(string A, string B) {
string t = A;
for (int i = ; i <= B.size() / A.size() + ; ++i) {
if (t.find(B) != string::npos) return i;
t += A;
}
return -;
}
};

下面这种解法还是由热心网友edyyy提供,没有用到字符串自带的find函数,而是逐个字符进行比较,循环字符串A中的所有字符,然后用个变量j,初始化为0,用来循环字符串B中的字符,每个字符和A中对应的字符进行比较,此时从A中取字符就要把A当作一个循环数组来处理,用(i+j)%m来取字符,还要确保j小于n,避免越界,如果字符匹配的话,j自增1。while 循环结束后,如果j等于n了,说明B中所有的字符都成功匹配了,那么我们来计算A的重复次数,通过(i+j-1)/m + 1来得到,注意i+j要减1再除以m,之后再加上一次。因为当i+j正好等于m时,说明此时不用重复A,那么(i+j-1)/m + 1还是等于1,当i+j>m的时候,需要重复A了,(i+j-1)/m + 1也可以得到正确的结构,参见代码如下:

解法三:

class Solution {
public:
int repeatedStringMatch(string A, string B) {
int m = A.size(), n = B.size();
for (int i = ; i < m; ++i) {
int j = ;
while (j < n && A[(i + j) % m] == B[j]) ++j;
if (j == n) return (i + j - ) / m + ;
}
return -;
}
};

类似题目:

Repeated Substring Pattern

参考资料:

https://discuss.leetcode.com/topic/105579/c-4-lines-o-m-n-o-1-and-8-lines-kmp-o-m-n-o-n

https://discuss.leetcode.com/topic/105566/java-solution-just-keep-building-oj-missing-test-cases

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Repeated String Match 重复字符串匹配的更多相关文章

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

  2. Leetcode686.Repeated String Match重复叠加字符串匹配

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

  3. LeetCode Repeated String Match

    原题链接在这里:https://leetcode.com/problems/repeated-string-match/description/ 题目: Given two strings A and ...

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

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

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

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

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

  7. 【Leetcode_easy】686. Repeated String Match

    problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...

  8. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  9. LeetCode Golang 3. 无重复字符的最长子串

    3. 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串 ...

随机推荐

  1. Matlab绘图基础——axis设置坐标轴取值范围

    peaks; axis tight  %Set the axis limits to equal the range of the data  axis square axis 'auto x'  % ...

  2. Python读取配置文件,并连接数据库SQL Server

    用配置文件保存固定的连接数据,改的话比较方便. 1.新建一个配置文件:SQlconfig.config,以数据库为例. 内容如下,当然也可以添加多个 [Database1] database=db_t ...

  3. spring框架学习笔记5:SpringAOP示例

    1.导包: 导入spring中的这两个包 再导入其他包(网上下载): 2.准备目标对象: package service; public class UserServiceImpl implement ...

  4. node初始

    ### 一.什么是node.js > Node是一个基于 Chrome V8 引擎的 JavaScript 运行环境 > > Node使用了一个事件驱动.非阻塞式 I/O 的模型,使 ...

  5. 解决办法:由于oracle版本不同导致导入数据时失败

    在向一个数据库导入dmp文件时,出现了如下错误 经查询,是由于"导出的dmp文件与导入的数据库的版本不同造成的" 用notepad查看dmp文件的版本,看看是否和数据库版本一致 解 ...

  6. NetFPGA-1G-CML Demo --- reference_router_nf1_cml

    环境 deepin 15.4 vivado 15.2 ise 14.6 前期准备 Github Wiki链接:https://github.com/NetFPGA/NetFPGA-public/wik ...

  7. Twisted 延迟调用

    延迟(defer)是twisted框架中实现异步的编程体系,使程序设计可以采用事件驱动的机制 1.基本使用 defer可以看作一个管理回调函数的对象,可以向该对象添加需要的回调函数同时也可以指定该组函 ...

  8. 利用python实现简单登陆注册系统

    #!/usr/bin/env python # -*- coding:utf-8 -*- def login(username,password): ''' :param username:用户名 : ...

  9. Huginn实现自动通过slack推送豆瓣高分电影

    博客搬迁至https://blog.wangjiegulu.com RSS订阅:https://blog.wangjiegulu.com/feed.xml 原文链接:https://blog.wang ...

  10. windows安装虚拟主机virtualbox遇到的困难

    本来想到可以在windows安装虚拟主机virtualbox,但是怎么自己的windows是盗版的,由于主题已经被破解了,所以不能安装结果强制性的进入pe然后从网上下载的dll文件复制到 c/wind ...