当前位置:网站首页>[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
边栏推荐
- PostgreSQL create database
- 项目经理如何击退被工作汇报支配的恐惧感?
- Understand the dense support functions / stored procedures of opengauss
- Laravel 8 realizes database backup through task scheduling
- (湖南科技大学oj作业)问题 G: 串的模式匹配
- It's really not human to let the express delivery arrive before the refund
- GO语言-Slice切片
- CLP information - financial standardization "14th five year plan" highlights data security
- Connect to the database using GSQL
- List和Dict数据类型作用详解
猜你喜欢

Customized thread communication (lock) of JUC
Detailed explanation of MySQL binlog log and master-slave replication

Overview and example analysis of opengauss database performance tuning

Deep separable convolution

How does the taskbar under the computer display open programs

Laravel 2020-01-01t00:00:00.000000z date conversion

What happened to the frequent disconnection of the computer at home

项目工作区创建步骤-泽众AR自动化测试工具

Nat Commun|語言模型可以學習複雜的分子分布

List和Dict数据类型作用详解
随机推荐
Database design recommendations
AI function of cutting-edge technology exploration: slow SQL discovery
Nat Commun|语言模型可以学习复杂的分子分布
Operation guide | how to select a collector on moonbeam and Moonriver
MySQL quick start instance (no loss)
CLP information - No. 1 central document on Strengthening Rural Revitalization financial services
laravel 8 通过 任务调度 实现 数据库备份
Nielseniq announces appointment of Tracey Massey as chief operating officer
PyQt5 使QPlainTextEdit控件支持行号显示
Hands on, how should selenium deal with pseudo elements?
Code farming essential SQL tuning (Part 2)
Frontier technology exploration deepsql: in Library AI algorithm
With an average annual salary of 20W, automated test engineers are so popular?
Connect to the database using GSQL
MSDN download win11 method, simple and easy to operate
Opengauss database ODBC environment connection configuration (Windows)
Elk enterprise log analysis system
one hundred and twenty-three thousand one hundred and twenty-three
Application of AI in index recommendation
Opengauss database JDBC environment connection configuration (eclipse)