当前位置:网站首页>214. minimum palindrome string
214. minimum palindrome string
2022-07-01 03:42:00 【Sun_ Sky_ Sea】
214. The shortest palindrome string
Original title link :https://leetcode.cn/problems/shortest-palindrome/
Given a string s, You can convert a string to a palindrome string by adding characters in front of it . Find and return the shortest palindrome string that can be converted in this way .
Example 1:
Input :s = “aacecaaa”
Output :“aaacecaaa”
Example 2:
Input :s = “abcd”
Output :“dcbabcd”
Tips :
0 <= s.length <= 5 * 104
s It's only made up of lowercase letters
Their thinking :
Use the string hash algorithm , find s The longest prefix palindrome substring of , The way to find it is to use the hash algorithm , Calculate whether the hash values of prefix substring and reverse prefix substring are equal , Equality is palindrome string . The title requires the string to be added , Namely s-s The longest prefix substring of .
You need to remember two formulas :
#s The hash value formula of the prefix string of
left = (left * base + ord(s[i])) % mod
#s Inverse prefix string hash value formula
right = (right + mul * ord(s[i])) % mod
Code implementation :
class Solution:
def shortestPalindrome(self, s: str) -> str:
n = len(s)
# Set up base Calculate the hash value , Set the modulus so that the result of the hash value is represented in a controllable range
base, mod = 131, 10**9 + 7
# left Indicates the prefix hash value ,right Indicates the reverse prefix hash value
left = right = 0
# Storage base The power of
mul = 1
# s The longest palindrome string in corresponds to s The subscript , Initialize to -1
best = -1
# from s Start search with the header of
for i in range(n):
# s The hash value formula of the prefix string of
left = (left * base + ord(s[i])) % mod
# s Inverse prefix string hash value formula
right = (right + mul * ord(s[i])) % mod
# If the hash values are equal, it means that it is a palindrome string
if left == right:
best = i
# Update power
mul = mul * base % mod
# s - s The reverse order of the longest palindrome prefix string of , Is the string to be added
add = ("" if best == n - 1 else s[best+1:])
return add[::-1] + s
reference :
https://leetcode.cn/problems/shortest-palindrome/solution/zui-duan-hui-wen-chuan-by-leetcode-solution/
边栏推荐
- 详解Spark运行模式(local+standalone+yarn)
- The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
- Feign remote call and getaway gateway
- Finally in promise
- 在线公网安备案保姆级教程【伸手党福利】
- Implement pow (x, n) function
- Md5sum operation
- 187. 重复的DNA序列
- Jeecgboot output log, how to use @slf4j
- Download and installation configuration of cygwin
猜你喜欢

bootsrap中的栅格系统
![4. [WebGIS practice] software operation chapter - data import and processing](/img/5a/b86e0538660f27c809cf429053a06c.png)
4. [WebGIS practice] software operation chapter - data import and processing

用小程序的技术优势发展产业互联网

Implement pow (x, n) function
![[nine day training] content III of the problem solution of leetcode question brushing Report](/img/7e/1e76181e56ef7feb083f9662df71c7.jpg)
[nine day training] content III of the problem solution of leetcode question brushing Report

The combination of applet container technology and IOT

【TA-霜狼_may-《百人计划》】2.1 色彩空间

Data exchange JSON

Edlines: a real time line segment detector with a false detection control

LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
随机推荐
二叉树神级遍历:Morris遍历
详解Spark运行模式(local+standalone+yarn)
Bilinear upsampling and f.upsample in pytorch_ bilinear
Leetcode:829. 连续整数求和
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
214. 最短回文串
完全背包问题
Valid brackets (force deduction 20)
【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍
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
The shell script uses two bars to receive external parameters
You cannot right-click F12 to view the source code solution on the web page
Avalanche problem and the use of sentinel
Gorilla/mux framework (RK boot): RPC error code design
数据库DDL(Data Definition Language,数据定义语言)知识点
jeecgboot输出日志,@Slf4j的使用方法
Cookie&Session
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
【TA-霜狼_may-《百人计划》】2.1 色彩空间
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理