当前位置:网站首页>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
}
}
};
边栏推荐
猜你喜欢
![[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born](/img/70/d275009134fcbf9ae984c0f278659e.jpg)
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born

软件卸载时遇到trying to use is on a network resource that is unavailable

UnsupportedOperationException异常

深度剖析C语言数据在内存中的存储

egg. JS project deployment online server

Generator parameters incoming parameters

Esp8266-rtos IOT development

Restful API design specification

TP-LINK 企业路由器 PPTP 配置

Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
随机推荐
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
PC easy to use essential software (used)
After reading the programmer's story, I can't help covering my chest...
[MySQL] limit implements paging
To effectively improve the quality of software products, find a third-party software evaluation organization
LeetCode:214. 最短回文串
Philosophical enlightenment from single point to distributed
Export IEEE document format using latex
opencv+dlib实现给蒙娜丽莎“配”眼镜
Restful API design specification
Shift Operators
Using C language to complete a simple calculator (function pointer array and callback function)
pytorch查看张量占用内存大小
随手记01
Current situation and trend of character animation
vb. Net changes with the window, scales the size of the control and maintains its relative position
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)
vb.net 随窗口改变,缩放控件大小以及保持相对位置
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges