当前位置:网站首页>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
}
}
};
边栏推荐
- sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
- UML图记忆技巧
- Unsupported operation exception
- 多元聚类分析
- Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
- Revit secondary development Hof method calls transaction
- LeetCode:836. 矩形重叠
- C语言深度解剖——C语言关键字
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- Problems in loading and saving pytorch trained models
猜你喜欢
win10系统中的截图,win+prtSc保存位置
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
使用latex导出IEEE文献格式
角色动画(Character Animation)的现状与趋势
C语言双指针——经典题型
C language double pointer -- classic question type
深度剖析C语言数据在内存中的存储
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
MongoDB 的安装和基本操作
sublime text的编写程序时的Tab和空格缩进问题
随机推荐
pytorch查看张量占用内存大小
LeetCode:劍指 Offer 42. 連續子數組的最大和
有效提高软件产品质量,就找第三方软件测评机构
ESP8266-RTOS物联网开发
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
View computer devices in LAN
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
Screenshot in win10 system, win+prtsc save location
Pytorch view tensor memory size
Computer graduation design PHP Zhiduo online learning platform
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
Sublime text using ctrl+b to run another program without closing other runs
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
[NVIDIA development board] FAQ (updated from time to time)
Tcp/ip protocol
CSP first week of question brushing
Export IEEE document format using latex
LeetCode:836. 矩形重叠
Double pointeur en langage C - - modèle classique