当前位置:网站首页>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
}
}
};
边栏推荐
- Crash problem of Chrome browser
- Deep analysis of C language pointer
- After reading the programmer's story, I can't help covering my chest...
- @JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
- ESP8266-RTOS物联网开发
- Simple use of promise in uniapp
- Problems in loading and saving pytorch trained models
- View computer devices in LAN
- CSP first week of question brushing
- hutool优雅解析URL链接并获取参数
猜你喜欢
C語言雙指針——經典題型
Restful API design specification
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
Esp8266-rtos IOT development
MySQL uninstallation and installation methods
UML图记忆技巧
View computer devices in LAN
Roguelike game into crack the hardest hit areas, how to break the bureau?
egg. JS project deployment online server
Generator parameters incoming parameters
随机推荐
个人电脑好用必备软件(使用过)
随手记01
Niuke winter vacation training 6 maze 2
UML diagram memory skills
Deep analysis of C language data storage in memory
egg. JS project deployment online server
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
Detailed explanation of heap sorting
Mobile phones and computers on the same LAN access each other, IIS settings
visdom可视化实现与检查介绍
数学建模2004B题(输电问题)
Shift Operators
Li Kou daily question 1 (2)
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
深度剖析C语言数据在内存中的存储
sublime text的编写程序时的Tab和空格缩进问题
After reading the programmer's story, I can't help covering my chest...