当前位置:网站首页>模拟卷Leetcode【普通】1062. 最长重复子串
模拟卷Leetcode【普通】1062. 最长重复子串
2022-07-06 06:15:00 【邂逅模拟卷】
汇总:模拟卷Leetcode 题解汇总
1062. 最长重复子串
给定字符串 S,找出最长重复子串的长度。如果不存在重复子串就返回 0。
示例 1:
输入:“abcd”
输出:0
解释:没有重复子串。
示例 2:
输入:“abbaba”
输出:2
解释:最长的重复子串为 “ab” 和 “ba”,每个出现 2 次。
示例 3:
输入:“aabcaabdaab”
输出:3
解释:最长的重复子串为 “aab”,出现 3 次。
示例 4:
输入:“aaaaa”
输出:4
解释:最长的重复子串为 “aaaa”,出现 2 次。
提示:
字符串 S 仅包含从 ‘a’ 到 ‘z’ 的小写英文字母。
1 <= S.length <= 1500
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-repeating-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def search(self, L: int, a: int, modulus: int, n: int, nums: List[int]) -> str:
""" Rabin-Karp with polynomial rolling hash. Search a substring of given length that occurs at least 2 times. @return start position if the substring exits and -1 otherwise. """
# compute the hash of string S[:L]
h = 0
for i in range(L):
h = (h * a + nums[i]) % modulus
# already seen hashes of strings of length L
seen = {
h}
# const value to be used often : a**L % modulus
aL = pow(a, L, modulus)
for start in range(1, n - L + 1):
# compute rolling hash in O(1) time
h = (h * a - nums[start - 1] * aL + nums[start + L - 1]) % modulus
if h in seen:
return start
seen.add(h)
return -1
def longestRepeatingSubstring(self, S: str) -> str:
n = len(S)
# convert string to array of integers
# to implement constant time slice
nums = [ord(S[i]) - ord('a') for i in range(n)]
# base value for the rolling hash function
a = 26
# modulus value for the rolling hash function to avoid overflow
modulus = 2 ** 24
# binary search, L = repeating string length
left, right = 1, n
while left <= right:
L = left + (right - left) // 2
if self.search(L, a, modulus, n, nums) != -1:
left = L + 1
else:
right = L - 1
return left - 1
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Overview of three core areas of Mathematics: geometry
- 数据库-当前读与快照读
- [C language] string left rotation
- 10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
- MFC关于长字符串unsigned char与CString转换及显示问题
- [C language] qsort function
- Left matching principle of joint index
- [wechat applet] build a development tool environment
- Construction and integration of Zipkin and sleuth for call chain monitoring
- 【Postman】Collections-运行配置之导入数据文件
猜你喜欢

Request forwarding and redirection

【Postman】Collections配置运行过程

功能安全之故障(fault),错误(error),失效(failure)
![[postman] collections - run the imported data file of the configuration](/img/85/7ac9976fb09c465c88f376b2446517.png)
[postman] collections - run the imported data file of the configuration

数据库隔离级别

数学三大核心领域概述:代数

(中)苹果有开源,但又怎样呢?

ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference

Detailed explanation of BF and KMP

全链路压测:构建三大模型
随机推荐
MySQL之基础知识
Usage of test macro of GTEST
Isam2 operation process
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
[C language] string left rotation
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
二维码的前世今生 与 六大测试点梳理
Seven imperceptible truths in software testing
D - How Many Answers Are Wrong
数字三角形模型 AcWing 1015. 摘花生
异常检测方法总结
LeetCode 732. 我的日程安排表 III
数学三大核心领域概述:代数
Summary of anomaly detection methods
MFC关于长字符串unsigned char与CString转换及显示问题
Understanding of processes and threads
把el-tree选中的数组转换为数组对象
MySQL之数据类型
php使用redis实现分布式锁
Linux regularly backs up MySQL database