当前位置:网站首页>LeetCode:214. Shortest palindrome string
LeetCode:214. Shortest palindrome string
2022-07-06 08:51:00 【Bertil】
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 * 10^4
- s It's only made up of lowercase letters
### Their thinking 1. First invert the string s, Then traverse the original string , Judge whether the string is palindrome string according to whether the string is equal before and after inversion , So as to find the shortest palindrome string in the original string 2. Finally, directly return the added character plus the original string ( The added character is the string before the shortest palindrome string in the original string )
Code
/** * @param {string} s * @return {string} */
var shortestPalindrome = function(s) {
const len = s.length
if (len === 0) return ''
// Reverse string s
let rev = s.split('').reduce((a, b) => b + a, '')
for (let i = 0; i < len; i++) {
// Judge whether the string is palindrome string according to whether the string is equal before and after inversion 【rev.slice(i) Namely s.slice(0, len - i) Inverted string of 】
if (s.slice(0, len - i) === rev.slice(i)) {
// If it is palindrome string , That is, the shortest palindrome string in the original string
return rev.slice(0, i) + s // Directly return the added character plus the original string
}
}
};
边栏推荐
- poi追加写EXCEL文件
- Sublime text using ctrl+b to run another program without closing other runs
- UnsupportedOperationException异常
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
- vb. Net changes with the window, scales the size of the control and maintains its relative position
- LeetCode:387. 字符串中的第一个唯一字符
- Excellent software testers have these abilities
- Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
- hutool优雅解析URL链接并获取参数
猜你喜欢

MongoDB 的安装和基本操作

opencv+dlib实现给蒙娜丽莎“配”眼镜

优秀的软件测试人员,都具备这些能力
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

Restful API design specification

ESP8266-RTOS物联网开发

Export IEEE document format using latex

egg. JS getting started navigation: installation, use and learning

sublime text中conda环境中plt.show无法弹出显示图片的问题

Detailed explanation of heap sorting
随机推荐
Alibaba cloud server mining virus solution (practiced)
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
UML图记忆技巧
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
数学建模2004B题(输电问题)
LeetCode:39. 组合总和
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
MYSQL卸载方法与安装方法
[MySQL] multi table query
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
Computer cleaning, deleted system files
Delay initialization and sealing classes
C语言深度解剖——C语言关键字
C语言双指针——经典题型
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
如何进行接口测试测?有哪些注意事项?保姆级解读
To effectively improve the quality of software products, find a third-party software evaluation organization
使用latex导出IEEE文献格式
深度剖析C语言指针