当前位置:网站首页>[leetcode daily question] Repeat overlay string matching
[leetcode daily question] Repeat overlay string matching
2022-06-11 16:10:00 【AI application Algorithm Engineer (ai+ security)】
Topic link
https://leetcode-cn.com/problems/repeated-string-match
Title Description
questions : secondary
Given two strings a and b, Look for overlapping strings a The minimum number of times , Make string b Becomes the superimposed string a The string of , Returns if it does not exist -1.
Be careful : character string "abc" Repeat stack 0 Next is "", Repeat stack 1 Next is "abc", Repeat stack 2 Next is "abcabc".
Their thinking
Python It's easy to match strings in , Call directly x.find(y) You can find it y stay x Position in .
But we need to put a Repeat the stack several times , only There may be Contains substrings b( It may never include ), So you can't stack indefinitely a, But to find a reasonable upper bound k , bring If a Repeat stack k Next time b Still not a substring , Then no matter how many times it is stacked , b It will never be a substring .
So the core here is to determine the upper bound k, There should be a GIF chart , That makes sense , But because of GIF Made with ( No ) some ( Meeting ) hemp ( system ) Bother ( do ), So we can draw a conclusion directly :
You can make len(a) * k >= len(a) + len(b) Established k It's all possible .
So just repeat the stack a, Until its length is greater than len(a) + len(b), And then call .find() You can find it b Position in the stack string , This position can be used to determine how many times the stack can contain 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
# Found using after submission temp.find(b) Method takes time 84ms,
# The following direct traversal only needs 40ms, So use traversal instead
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
边栏推荐
- Learn automatic testing of postman interface from 0 to 1
- Yiwenjiaohui your database system tuning
- [Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster
- 无心剑英汉双语诗001. 《春游》
- How AGC security rules simplify user authorization and authentication requests
- dapr 思维导图
- PostgreSQL create database
- Collection | can explain the development and common methods of machine learning!
- Learn how to parse SQL from kernel code
- Go language - value types and reference types
猜你喜欢

Classmate, have you heard of mot?

Laravel listening mode

Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library

How to optimize the performance of compose? Find the answer through "underlying principles" | developers say · dtalk

Yef 2022 opened yesterday. The whole process of free live broadcast on multiple network platforms opened an online technology feast!

Discussion on opengauss parallel decoding

This "invisible" robot may be your future colleague

如何优化 Compose 的性能?通过「底层原理」寻找答案 | 开发者说·DTalk

Thales cloud security report shows that cloud data leakage and complexity are on the rise

laravel 2020-01-01T00:00:00.000000Z 日期转化
随机推荐
laravel 2020-01-01T00:00:00.000000Z 日期转化
时间复杂度与空间复杂度解析
Go Language - value type and Reference Type
postgresql创建表
Nat Common | le Modèle linguistique peut apprendre des distributions moléculaires complexes
推开混合云市场大门,Lenovo xCloud的破局之道
[daily question series]: how to test web forms?
Opengauss database flashback function verification
Connect to the database using GSQL
Import data: GS_ restore or MERGE INTO? See which one suits you better
Database resource load management (Part 1)
wget命令使用
Import data to the database? Try the copy from stdin statement
Laravel8 implementation of sign in function
DHCP protocol instantiation analysis
What happened to the frequent disconnection of the computer at home
Streaking? Baa!
电脑下面的任务栏怎么显示打开的程序
How to manage concurrent write operations? Get you started quickly
Database design recommendations