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 ...
随机推荐
- 网络编程之多线程——GIL全局解释器锁
网络编程之多线程--GIL全局解释器锁 一.引子 定义: In CPython, the global interpreter lock, or GIL, is a mutex that preven ...
- centos下U盘重装windows
当前机器使用的都是centos系统,后来想使用windows,这时候就需要在centos下去安装windows系统啦. 当然是采用U盘安装啦,先准备U盘,U盘数据先备份以免丢失. 第一步:制作U盘启动 ...
- MySQL性能优化以及常用命令
1.将查询操作SELECT中WHERE条件后面和排序字段建立索引 2.按需查询,需要哪个字段就查哪个字段,禁止使用"SELECT * " 3.数据库引擎最好选用InnoDB,少用M ...
- 2018年蓝桥杯java b组第五题
标题:快速排序 以下代码可以从数组a[]中找出第k小的元素. 它使用了类似快速排序中的分治算法,期望时间复杂度是O(N)的. 请仔细阅读分析源码,填写划线部分缺失的内容. 我在使用(a, l, r, ...
- Centos7搭建Scrapy爬虫环境
写在前面 因为之前的爬虫环境一直是部署在我自己本地的电脑上的,最近,写了一个监控别人空间的爬虫,需要一直线上24小时运行,所有就打算云服务器上部署环境,也捣鼓了好一会才弄好,还是有一些坑,这里先记录一 ...
- linux centos安装zabbix 4.0服务端
1.服务器安装docker sudo yum install -y yum-utils device-mapper-persistent-data lvm2 sudo yum-config-manag ...
- 遇见Python
开发: 开发语言: 1.高级语言:Python Java.PHP C# Go ruby C++... ===> 字节码 2.低级语言:C.汇编 ...
- 基于SpringBoot+WebSocket搭建一个简单的多人聊天系统
前言 今天闲来无事,就来了解一下WebSocket协议.来简单了解一下吧. WebSocket是什么 首先了解一下WebSocket是什么?WebSocket是一种在单个TCP连接上进行全双工 ...
- Timed out after 30000 ms while waiting to connect
今天使用mongo-java-drive写连接mongo的客户端,着实被上面那个错坑了一把.回顾一下解决过程: 报错: com.mongodb.MongoTimeoutException: Timed ...
- web前端体系-了解前端,深入前端,架构前端,再看前端。大体系-知识-小细节
1.了解前端,深入前端,架构前端,再看前端.大体系-知识-小细节 个人认为:前端发展最终的导向是前端工程化,智能化,模块化,组件化,层次化. 2.面试第一关:理论知识. 2-1.http标准 2-2. ...