当前位置:网站首页>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/
边栏推荐
- Pathmeasure implements loading animation
- 【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
- 深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
- Complete knapsack problem
- 在 C 中声明函数之前调用函数会发生什么?
- 不用加减乘除实现加法
- The difference between MFC for static libraries and MFC for shared libraries
- [小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
- 10、Scanner. Next() cannot read spaces /indexof -1
- Online public network security case nanny level tutorial [reaching out for Party welfare]
猜你喜欢
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
Leetcode 128 longest continuous sequence (hash set)
MFC窗口滚动条用法
TEC: Knowledge Graph Embedding with Triple Context
Jeecgboot output log, how to use @slf4j
家居网购项目
服务器渲染技术jsp
pytorch nn.AdaptiveAvgPool2d(1)
Valentine's Day is nothing.
实现pow(x,n)函数
随机推荐
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
ECMAScript 6.0
Finally in promise
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
数据库中COMMENT关键字的使用
Detailed explanation of ES6 deconstruction grammar
Keil5中如何做到 0 Error(s), 0 Warning(s).
[nine day training] content III of the problem solution of leetcode question brushing Report
在 C 中声明函数之前调用函数会发生什么?
10、Scanner. Next() cannot read spaces /indexof -1
复习专栏之---消息队列
Cygwin的下载和安装配置
Use of comment keyword in database
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
数据库DDL(Data Definition Language,数据定义语言)知识点
Server rendering technology JSP
[reach out to Party welfare] developer reload system sequence
不用加减乘除实现加法
IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)