Leetcode686.Repeated String Match重复叠加字符串匹配
给定两个字符串 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重复叠加字符串匹配的更多相关文章
- LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...
- Leetcode 686.重复叠加字符串匹配
重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd", ...
- Java实现 LeetCode 686 重复叠加字符串匹配
686. 重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd&q ...
- 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 ...
- [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 ...
- Q686 重复叠加字符串匹配
给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = " ...
- [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 ...
- [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】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- ssh 与服务器保持连接
直接改客户端,服务器端不应该更改. sudo vi /etc/ssh/ssh_config # 或 ~/.ssh/config TCPKeepAlive=yes # Client每隔 60 秒发送一次 ...
- PAT甲级——A1074 Reversing Linked List
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- Java MySQL 批量查询数据,每次查询10条
因为 数据量比较多, 比如每次 /** * 批量查询 * @param sourList * @param batchCount * @param userMapper * @return */ pu ...
- MVC中视图访问的约定
通常访问视图的时候,都会去选择访问Views文件夹内对应于Controller同名的文件夹下的某一个视图,这个视图对应于这个Controller类的某一个方法. 其实,也可以让这个方法对应于不同名的c ...
- linux与window文件传输(使用ssh+putty)
linux与window文件传输(使用ssh+putty) https://blog.csdn.net/Imagine_Dragon/article/details/78303241
- 使用Log4net把日志写入到SqlServer数据库
1.官网URL: http://logging.apache.org/log4net/ 2.配置文件参照URL: http://logging.apache.org/log4net/release/c ...
- 适配的js
/** * Created by fanso2o on 2016/9/7. */$(document).ready(function () { function change() { var html ...
- [JSOI2010]连通数 (dfs或tarjan或bitset)+bitset学习
题目描述 输入格式 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示无边. 输出格式 输出一行一个整数,表示该图的连通数. 样例 样 ...
- OpenCV读取RTSP视频流
用opencv的VideoCapture读取RTSP视频流,只有opencv3.1版本可以,之前的版本都无法读取视频流.可能的原因是云平台的RTSP视频流太差,经常错码.项目最后使用的是opencv2 ...
- RN 环境搭建 运行demo App
1.环境搭建 1.1 JDK 1.2Android JDK 需要安装c++环境(我选择安装android studio) 1.3Node npm config set registry https: ...