当前位置:网站首页>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/
边栏推荐
- ECMAScript 6.0
- Feature Pyramid Networks for Object Detection论文理解
- Leetcode 1818 absolute value, sorting, dichotomy, maximum value
- Golang multi graph generation gif
- 【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
- 【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
- Appium fundamentals of automated testing - basic principles of appium
- Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
- Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
- ES6解构语法详解
猜你喜欢

Idea plug-in backup table

Error: plug ins declaring extensions or extension points must set the singleton directive to true

使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况

Implement pow (x, n) function

pytorch训练深度学习网络设置cuda指定的GPU可见

Feature Pyramid Networks for Object Detection论文理解

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)

快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内

详解Spark运行模式(local+standalone+yarn)

后台系统右边内容如何出现滚动条和解决双滚动条的问题
随机推荐
jeecgboot输出日志,@Slf4j的使用方法
后台系统右边内容如何出现滚动条和解决双滚动条的问题
Gorilla/mux framework (RK boot): RPC error code design
BluePrism注册下载并安装-RPA第一章
MFC窗口滚动条用法
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
4. [WebGIS practice] software operation chapter - data import and processing
ECMAScript 6.0
【快捷键】
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
ASGNet论文和代码解读2
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
不用加减乘除实现加法
Ridge regression and lasso regression
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
72. 编辑距离
The shell script uses two bars to receive external parameters
Appium自动化测试基础 — APPium基本原理
10. 正则表达式匹配
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st