当前位置:网站首页>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
}
}
};
边栏推荐
猜你喜欢
UnsupportedOperationException异常
优秀的软件测试人员,都具备这些能力
生成器参数传入参数
Compétences en mémoire des graphiques UML
可变长参数
Double pointeur en langage C - - modèle classique
【ROS】usb_ Cam camera calibration
Delay initialization and sealing classes
Mobile phones and computers on the same LAN access each other, IIS settings
Light of domestic games destroyed by cracking
随机推荐
pytorch查看张量占用内存大小
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
多元聚类分析
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
POI add write excel file
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Deep anatomy of C language -- C language keywords
[sword finger offer] serialized binary tree
View computer devices in LAN
深度剖析C语言指针
LeetCode:剑指 Offer 42. 连续子数组的最大和
Visual implementation and inspection of visdom
The harm of game unpacking and the importance of resource encryption
Esp8266-rtos IOT development
有效提高软件产品质量,就找第三方软件测评机构
704 binary search
ROS compilation calls the third-party dynamic library (xxx.so)
UML图记忆技巧
Problems in loading and saving pytorch trained models