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 ...
随机推荐
- Windows Server 2012更新补丁后导致Micosoft ODBC for Oracle出现问题
上周由于WannaCry勒索病毒肆虐全球,14号系统管理员更新了一系列补丁后,导致一Windows Server 2012服务器上面部署的一些老旧的应用无法使用Microsoft ODBC for O ...
- Powershell测试端口状态
function Test-Port { Param([string]$ComputerName,$port = 5985,$timeout = 1000) try { $tcpclient = Ne ...
- innerHTML的使用
inerHTML是html标签的属性,成对出现的标签大多数都有这个属性,用来设置或获取位于对象起始和结束标签 内的HTML.(获取HTML当前标签的起始和结束里面的内容)不包括标签本身. ...
- VBR的部署
一.实验拓扑图 二.实验目标 通过部署Veeam Backup & replication,实现虚拟机的备份和还原. 三实验要求 1. 如图所示,开启实验环境.(请参考公众号以前的相关文档) ...
- 【字符串】ZSC-字符串编辑
Description 从键盘输入一个字符串(长度<=40个字符),对字符串进行编辑.例如,输入"This is a book." 现对该字符串进行编辑,编辑功能有:(1) ...
- 获取当前页面的URL信息
以前在做网站的时候,经常会遇到当前页的分类高亮显示,以便让用户了解当前处于哪个页面.之前一直是在每个不同页面写方法.工程量大,也不便于修改.一直在想有什么简便的方法实现.后来在网上查到可以用获取当前U ...
- Web和移动开发的未来
Web和移动开发的未来 当我们与来自整个IT行业的专家交谈时,他们告诉我们技术,PWA和优化的持续整合是关键. 为了收集有关网络和移动开发当前和未来状况的见解,我们与19位IT主管进行了交流,我们问道 ...
- C++编写 动态链接库dll 和 调用dll
参考:https://jingyan.baidu.com/article/ff42efa92c49cfc19e2202fd.html 和htps://jingyan.baidu.com/article ...
- 【适合N卡独显电脑的环境配置】Tensorflow教程-Windows 10下安装tensorflow 1.5.0 GPU with Anaconda
注意: 1.目前Anaconda 更新原命令activate tensorflow 改为 conda activate tensorflow 2. 目前windows with anaconda 可以 ...
- 【转】如何使用分区助手完美迁移系统到SSD固态硬盘?
自从SSD固态硬盘出世以来,一直都被持续关注着,SSD的性能优势让无数用户起了将操作系统迁移到SSD的心思,直接后果就是让无数机械硬盘为止黯然退场,很多软件都可以做到系统迁移,然而,被完美迁移的系统却 ...