当前位置:网站首页>[LeetCode每日一题] |686.重复叠加字符串匹配
[LeetCode每日一题] |686.重复叠加字符串匹配
2022-06-11 15:50:00 【AI应用算法工程师(AI+安全)】
题目链接
https://leetcode-cn.com/problems/repeated-string-match
题目描述
题目难度:中等
给定两个字符串 a和 b,寻找重复叠加字符串 a的最小次数,使得字符串 b成为叠加后的字符串 a的子串,如果不存在则返回 -1。
注意:字符串 "abc" 重复叠加 0 次是 "",重复叠加 1 次是 "abc",重复叠加 2 次是 "abcabc"。
解题思路
Python里字符串匹配是很容易的,直接调用 x.find(y)即可找到 y在 x中的位置。
但本题中需要把 a重复叠加若干遍后,才 有可能 包含子串 b(也可能永远都不包含),因此不能无限的堆叠 a,而是要找到一个合理的上界 k ,使得 如果 a重复叠加 k 次后 b仍不是其子串,则无论叠加多少次, b永远不会是其子串。
因此这里的核心就是确定这个上界 k,此处应该有一个GIF图,就很好理解了,但由于GIF制作有(不)些(会)麻(制)烦(做),因此直接给出结论:
可以使 len(a) * k >= len(a) + len(b)成立的 k 都是可行的。
所以只要重复堆叠 a,直到其长度大于 len(a) + len(b),然后调用.find()即可找到 b在堆叠字符串中的位置,通过这个位置可以判断最少堆叠多少次后可以包含 b。
# Python3
class Solution:
def repeatedStringMatch(self, a: str, b: str) -> int:
res = len(b) // len(a) + 1
temp = ''
for _ in range(res):
temp += a
temp += a
# 提交后发现使用temp.find(b)方法需要耗时84ms,
# 而下面这种直接遍历只需要40ms,因此改用遍历
for i in range(len(temp) - len(b) + 1):
if b == temp[i: i + len(b)]:
x, y = divmod(i + len(b), len(a))
if y > 0:
x += 1
return x
return -1
return x
return -1
边栏推荐
- PostgreSQL create table
- Ai4db: AI slow SQL root cause analysis
- 使用Cloud DB构建APP 快速入门-快游戏篇
- Code farming essential SQL tuning (Part 2)
- What is the future of software testing in 2022? Do you need to understand the code?
- Kill the swagger UI. This artifact is better and more efficient!
- Learn how to parse SQL from kernel code
- [Yugong series] June 2022 Net architecture class 078 worker cluster of distributed middleware schedulemaster
- [Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster
- 前沿科技探究之AI在索引推荐的应用
猜你喜欢
![[Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster](/img/bc/694f8baec114cc3f6e35c4c76c29f0.png)
[Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster

数据库资源负载管理(下篇)

rf中安装的第四步:安装 robotframework-selenium2library报错

码农必备SQL调优(上)

Classmate, have you heard of mot?

From repeatedly rejected manuscripts to post-90s assistant professor, Wang Hao of Rutgers University: curiosity drives me to constantly explore

【愚公系列】2022年06月 .NET架构班 079-分布式中间件 ScheduleMaster的集群原理

Collect | thoroughly understand the meaning and calculation of receptive field

收藏 |彻底搞懂感受野的含义与计算

【0006】title、關鍵字及頁面描述
随机推荐
如何优化 Compose 的性能?通过「底层原理」寻找答案 | 开发者说·DTalk
Docker uses redis image
[0006] titre, mots clés et description de la page
CLP information - No. 1 central document on Strengthening Rural Revitalization financial services
CLP information - financial standardization "14th five year plan" highlights data security
From digital twinning to digital immortality, the "three-stage theory" of the development of the meta universe
【0006】title、關鍵字及頁面描述
postgresql创建数据库
openGauss简单查询SQL的执行流程解析
wget命令使用
Import data: GS_ restore or MERGE INTO? See which one suits you better
PostgreSQL create table
Why are bugs changing more and more?
openGauss数据库闪回功能验证
轻松上手使用gs_dump和gs_dumpall命令导出数据
泰雷兹云安全报告显示,云端数据泄露和复杂程度呈上升趋势
【愚公系列】2022年06月 .NET架构班 078-分布式中间件 ScheduleMaster的Worker集群
GO語言-值類型和引用類型
【愚公系列】2022年06月 .NET架构班 079-分布式中间件 ScheduleMaster的集群原理
Classmate, have you heard of mot?