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 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.
/*
上来的想法是KMP的思想,但是具体不会KMP的实现,c++STL的string 中的find?但是要注意A的长度与B的长度的问题,首先A的长度要大于B的长度
分为三个阶段: 1. A比B的长度要短,这时A要不断增加。 2. A 比 B 刚好长, 3, A 比 B 刚好长的长度 + 1个A的长度, 这时能够保证在A的原始的串中能够索引完一遍B的长度,如果此时没有找到那么就要返回-1
cdab cdab
abcd-abcd
abcd-abcd-abcd
time complexity ?
*/
class Solution {
public:
int repeatedStringMatch(string A, string B) {
int count = ;
string str;
while (str.size() < B.size()){ // 使A达到刚好比B长的情况,记录此时的重复次数
str.append(A);
count++;
}
if (str.find(B) != -){
return count;
}
str.append(A); // 再加一次A
if (str.find(B) != -){ // 保证A的所有的字符都能被B索引一次
return ++count;
}
return -;
}
};
Leetcode 686 Repeated String Match的更多相关文章
- [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_easy】686. Repeated String Match
problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 686. Repeated String Match 字符串重复后的子字符串查找
[抄题]: Given two strings A and B, find the minimum number of times A has to be repeated such that B i ...
- 686. Repeated String Match
方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...
- 686. Repeated String Match判断字符串重复几次可以包含另外一个
public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...
- 【刷题笔记】686. Repeated String Match
题意 题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A? 暴力解法 直接 A+A+...,到哪次 A 包含 B 了,就返 ...
- LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...
- 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 ...
随机推荐
- MySQL 5.7开启二进制日志注意事项
最近才开始将部分MySQL 5.6升级到MySQL 5.7, 在开启MySQL的二进制日志时,发现MySQL 5.7 与MySQL 5.6已有细微区别.如果在my.cnf配置文件中,只设置了全局系统变 ...
- [20181124]关于降序索引问题2.txt
[20181124]关于降序索引问题2.txt --//链接:blog.itpub.net/267265/viewspace-2221425/,探讨降序索引中索引的键值.--//实际上使用函数sys_ ...
- 关于wordpress慢的问题
随笔记载: 关于wordpress访问慢的原因,主要是wordpress采用了google前端库, 需替换wordpress/wp-includes/script-header.php中所有的goog ...
- Oracle的实例恢复解析
在数据库服务器异常断电重启后,数据库会进行实例恢复,那么实例恢复的过程中Oracle做了什么操作呢?参考官网在这里做一下解释,菜鸟水平有限,欢迎勘正. 首先说下实例恢复的定义: Instance re ...
- Android 闪烁动画
import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animatio ...
- c/c++赋值函数(重载=号运算符)
c/c++赋值函数(重载=号运算符) 首先c++里的各种运算符都是用函数实现的,比如=,就等号函数. 所以当用=给一个对象赋值的时候,实际调用的是=号所对应的=号函数. 分析下面的代码 #includ ...
- 如何指定一个计划和目标——6W
作为一名IT人员,需要自己指定一个计划和目标,来保证完成进度和高效的完成任务. 参考管理学如何制作计划和目标的,套用过来,也是同样适用的.来先看看管理学的相关知识吧. 计划的概念:计划是为实现组织目标 ...
- MySQL高级知识(十六)——小表驱动大表
前言:本来小表驱动大表的知识应该在前面就讲解的,但是由于之前并没有学习数据批量插入,因此将其放在这里.在查询的优化中永远小表驱动大表. 1.为什么要小表驱动大表呢 类似循环嵌套 for(int i=5 ...
- linux学习笔记整理(六)
第七章 Centos7-文件权限管理本节所讲内容:7.1文件的基本权限:r w x (UGO)7.2文件的特殊权限:suid sgid sticky和文件扩展权限ACL7.3实战:创建一个让root都 ...
- centos7下安装docker(18.3docker日志---logging driver---fluentd)
前面我们学的ELK中用filebeat收集docker容器日志,利用的是dcoker默认的logging driver json-file,下面我们用fluentd来收集容器日志 Fluentd是一个 ...