当前位置:网站首页>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
}
}
};
边栏推荐
- LeetCode:剑指 Offer 04. 二维数组中的查找
- TDengine 社区问题双周精选 | 第三期
- opencv+dlib实现给蒙娜丽莎“配”眼镜
- LeetCode:387. 字符串中的第一个唯一字符
- 如何进行接口测试测?有哪些注意事项?保姆级解读
- Charging interface docking tutorial of enterprise and micro service provider platform
- vb. Net changes with the window, scales the size of the control and maintains its relative position
- UML圖記憶技巧
- LeetCode:836. 矩形重叠
- 如何有效地进行自动化测试?
猜你喜欢

C language double pointer -- classic question type

Export IEEE document format using latex

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

sublime text的编写程序时的Tab和空格缩进问题

Image,cv2读取图片的numpy数组的转换和尺寸resize变化

After reading the programmer's story, I can't help covering my chest...

visdom可视化实现与检查介绍

PC easy to use essential software (used)

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

Sublime text using ctrl+b to run another program without closing other runs
随机推荐
[embedded] cortex m4f DSP Library
Deep analysis of C language pointer
Niuke winter vacation training 6 maze 2
TP-LINK 企业路由器 PPTP 配置
【剑指offer】序列化二叉树
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
Current situation and trend of character animation
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
UnsupportedOperationException异常
PC easy to use essential software (used)
有效提高软件产品质量,就找第三方软件测评机构
win10系统中的截图,win+prtSc保存位置
力扣每日一题(二)
Revit 二次开发 HOF 方式调用transaction
LeetCode:498. 对角线遍历
LeetCode:剑指 Offer 42. 连续子数组的最大和
LeetCode:26. 删除有序数组中的重复项
hutool优雅解析URL链接并获取参数
[embedded] print log using JLINK RTT
优秀的软件测试人员,都具备这些能力