当前位置:网站首页>214. 最短回文串
214. 最短回文串
2022-07-01 03:23:00 【Sun_Sky_Sea】
214. 最短回文串
原始题目链接:https://leetcode.cn/problems/shortest-palindrome/
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
示例 1:
输入:s = “aacecaaa”
输出:“aaacecaaa”
示例 2:
输入:s = “abcd”
输出:“dcbabcd”
提示:
0 <= s.length <= 5 * 104
s 仅由小写英文字母组成
解题思路:
使用字符串哈希算法,找到s的最长前缀回文子串,找的方式是使用哈希算法,计算前缀子串和反序前缀子串的哈希值是否相等,相等就是回文串。题目要求的是需要添加的字符串,就是s-s的最长前缀子串。
需要记住两个公式:
#s的前缀串的哈希值公式
left = (left * base + ord(s[i])) % mod
#s的反序前缀串哈希值公式
right = (right + mul * ord(s[i])) % mod
代码实现:
class Solution:
def shortestPalindrome(self, s: str) -> str:
n = len(s)
# 设置base计算哈希值,设置模为了哈希值的结果表示在一个可控的范围
base, mod = 131, 10**9 + 7
# left表示前缀哈希值,right表示反序前缀哈希值
left = right = 0
# 存储base的幂
mul = 1
# s中最长的回文串对应在s的下标,初始化为-1
best = -1
# 从s的头开始查找
for i in range(n):
# s的前缀串的哈希值公式
left = (left * base + ord(s[i])) % mod
# s的反序前缀串哈希值公式
right = (right + mul * ord(s[i])) % mod
# 如果哈希值相等表示是回文串
if left == right:
best = i
# 更新幂
mul = mul * base % mod
# s - s的最长回文前缀串的反序串,就是要添加的字符串
add = ("" if best == n - 1 else s[best+1:])
return add[::-1] + s
参考文献:
https://leetcode.cn/problems/shortest-palindrome/solution/zui-duan-hui-wen-chuan-by-leetcode-solution/
边栏推荐
- File upload and download
- Leetcode: offer 59 - I. maximum value of sliding window
- Pyramid scene parsing network [pspnet] thesis reading
- You cannot right-click F12 to view the source code solution on the web page
- 【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
- [nine day training] content III of the problem solution of leetcode question brushing Report
- Leetcode 1818 absolute value, sorting, dichotomy, maximum value
- [深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
- bootsrap中的栅格系统
- 后台系统右边内容如何出现滚动条和解决双滚动条的问题
猜你喜欢

Home online shopping project

文件上传下载

整合阿里云短信的问题:无法从静态上下文中引用非静态方法

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
![[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation

Cookie&Session

pytorch nn. AdaptiveAvgPool2d(1)

后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动

FCN全卷积网络理解及代码实现(来自pytorch官方实现)

Binary tree god level traversal: Morris traversal
随机推荐
Addition without addition, subtraction, multiplication and division
在 C 中声明函数之前调用函数会发生什么?
Detailed explanation of ES6 deconstruction grammar
BluePrism注册下载并安装-RPA第一章
409. 最长回文串
Thread data sharing and security -threadlocal
Appium fundamentals of automated testing - basic principles of appium
Online public network security case nanny level tutorial [reaching out for Party welfare]
leetcode 1818 绝对值,排序,二分法,最大值
Valentine's Day is nothing.
网页不能右键 F12 查看源代码解决方案
Feature pyramid networks for object detection
小程序容器技术与物联网IoT的结合点
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
Cookie&Session
Leetcode:829. 连续整数求和
Keil5中如何做到 0 Error(s), 0 Warning(s).
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
Valid brackets (force deduction 20)