当前位置:网站首页>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
}
}
};
边栏推荐
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
- PC easy to use essential software (used)
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- [OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
- TP-LINK 企业路由器 PPTP 配置
- Nacos 的安装与服务的注册
- egg. JS project deployment online server
- Leetcode刷题题解2.1.1
- [MySQL] multi table query
猜你喜欢
Navicat premium create MySQL create stored procedure
Mongodb installation and basic operation
个人电脑好用必备软件(使用过)
UML圖記憶技巧
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
TCP/IP协议
egg. JS project deployment online server
TP-LINK enterprise router PPTP configuration
Delay initialization and sealing classes
Trying to use is on a network resource that is unavailable
随机推荐
What are the common processes of software stress testing? Professional software test reports issued by companies to share
Computer graduation design PHP Zhiduo online learning platform
Problems encountered in connecting the database of the project and their solutions
The mysqlbinlog command uses
Shift Operators
如何正确截取字符串(例:应用报错信息截取入库操作)
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Navicat Premium 创建MySql 创建存储过程
egg. JS project deployment online server
Cesium draw points, lines, and faces
Purpose of computer F1-F12
704 binary search
LeetCode:剑指 Offer 03. 数组中重复的数字
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
opencv+dlib实现给蒙娜丽莎“配”眼镜
Crash problem of Chrome browser
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
LeetCode:162. 寻找峰值
Li Kou daily question 1 (2)
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs