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 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.
理解:
给出两个字符串,找到A重复的最小次数,使得B字符串是A重复后的子字符串,如果没有答案则返回-1。
举个例子,A="abcd",B = "cdabcdab",返回结果 3, 因为A重复三次以后,"abcdabcdabcd",此时B就是A的一个子字符串了,而且3是最小的重复次数,因为重复两次的时候“abcdabcd”不能使B成为A的子字符串。
注意:A和B字符串的长度在1到10000之间
原始解题思路:
建立一个循环,记录循环次数,拼接A字符串为C,然后判断B是否在C内,如果存在则输出循环次数,如果不同,则继续循环。
如果C的长度大于10000,则返回-1。
python 代码:
def repeatedStringMatch(A, B):
i = 1
C = A
while(1):
if B in C:
return i
else:
i = i + 1
C = C + A
if len(C) > 10000:
return -1
验证结果:
- 尽可能减少循环次数
n = 1
n = 2
def repeatedStringMatch(A, B):
C = ""
for i in range(int(len(B)/len(A))+ 3):
if B in C:
return i
C += A
return -1
验证结果:
LeetCode686——Repeated String Match的更多相关文章
- Leetcode686.Repeated String Match重复叠加字符串匹配
给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = " ...
- LeetCode 686. 重复叠加字符串匹配(Repeated String Match)
686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...
- 【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 ...
- [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 ...
- [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 ...
- 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 ...
- LeetCode Repeated String Match
原题链接在这里:https://leetcode.com/problems/repeated-string-match/description/ 题目: Given two strings A and ...
随机推荐
- Kubernetes学习之Kubernetes架构
架构 Kubernetes历史
- [AI] 深度数据 - Data
Data Engineering Data Pipeline Outline [DE] How to learn Big Data[了解大数据] [DE] Pipeline for Data Eng ...
- [Vue warn]: Duplicate keys detected: 'area'. This may cause an update error.
运行vue程序,浏览器报错: 原因:检测到重复的密钥:'area',因为在使用v-for循环绑定的时候,key的值是唯一的,不能相同,否则会出现意想不到的bug 解决办法:v-for时绑定的key唯一
- mysql创建表时字段类型选择与优化
一.选择原则 1.应该尽量使用可以正确存储数据的最小字段类型 2.选用简单的数据类型,例如:一个是尽量用mysql内置的字段类型来存储日期和时间:另一个存储IP地址尽量用整型:能用整型的尽量不用字符串 ...
- docker容器添加对外映射端口
一般在运行容器时,我们都会通过参数 -p(使用大写的-P参数则会随机选择宿主机的一个端口进行映射)来指定宿主机和容器端口的映射,例如 docker run -it -d --name [contain ...
- thymeleaf常用配置说明
#spring.thymeleaf.cache = true #启用模板缓存. #spring.thymeleaf.check-template = true #在呈现模板之前检查模板是否存在. #s ...
- Nodejs 发送邮件 激活邮箱
1. 安装nodemailer npm install nodemailer 项目中引入nodemailer var nodemailer = require('nodemailer'); 2.QQ邮 ...
- Yii2 前后台登陆退出分离、登陆验证
这里用的yii2高级模板, 基本模板的配置文件在一个文件里,方法基本没什么区别, 1.用户表要有两个用户表, 当然一个也行,分开是省得麻烦,既然是分离了就彻底分开, 前台表user,后台表user_b ...
- jQuery查找节点(选择器)
jQuery通过选择器来完成节点的查找: 1.基本选择器: ①通用/所有的选择器:$("*") //使用*号来表示. ②:标签选择器:$("标签名(div)") ...
- SpringBootSecurity学习(21)前后端分离版之OAuth2.0非对称加密
JWT转换器 前面的例子中,都是在授权服务配置类中配置了一个很简单的jwt转换器,如下: 可以看到我们只用setSigningKey方法配置了一个秘钥,这里使用的是简单的对称加密的方式来加密jwt内容 ...