[LeetCode][JavaScript]Shortest Palindrome

Shortest Palindrome

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.

For example:

Given "aacecaaa", return "aaacecaaa".

Given "abcd", return "dcbabcd".

https://leetcode.com/problems/shortest-palindrome/



没有用到高贵的KMP算法。

看上去是O(n^2)的复杂度,但最后效果还挺好,140ms,有很多情况下都可以break掉。

主要的思路是取一个点,也就是期望的回文中心,把head和tail都指向它,先找前后相同的数,调整指针位置,然后head--, tail++这样找回文。

如果head等于0,说明找到了,倒着输出tail后面的字符加上input的字符串就是答案。

然后这个回文中心的问题,字符串中间和中间之前的点才有可能是回文的中心,后一半可以排除。

回文中心可能是单个或多个字母,如果是多个字母,他们肯定都是相同的。

举栗子:"dabbbbbaac", H表示head,T表示tail

1. a     a     b     b     b     b     b     a     a     c      --一开始都指向中间

HT

2. a     a     b     b     b     b     b     a     a     c      --左右找相同的元素

H                         T

3. a     a     b     b     b     b     b     a     a     c      --相同,继续找

H                                      T

3. a     a     b     b     b     b     b     a     a     c      --找到前缀了, 输出c+原始的字符串

H                                                   T

https://leetcode.com/discuss/36978/simple-javascript-o-n-2-solution-140ms

The idea of this solution is trying to find the palindrome center.

We have 2 pointers ‘head‘ and ‘tail pointing to the expected center.

At the begining of each loop, we find neighbors which have the same value, then adjust the pointers.

If s[head] is equals to the s[tail], head--, tail++.

If head is equals to 0, the result is the inverted string behind ‘tail‘.

Noticed that 1. Palindrome center only existing in the first half of the string.

2. If the center is not a single character,  they should be same letters.

 1 /**
 2  * @param {string} s
 3  * @return {string}
 4  */
 5 var shortestPalindrome = function(s) {
 6     var prefix = "";
 7     var pos, head, tail;
 8
 9      for(pos = head = tail = parseInt(s.length / 2); pos > 0; head = tail = --pos){
10         while(head !== 0 && s[head - 1] === s[head]){
11             head--; pos--;
12         }
13         while(tail != s.length - 1 && s[tail + 1] === s[tail]){
14             tail++;
15         }
16         var isSame = true;
17         while(head >= 0){
18             if(s[head] !== s[tail]){
19                 isSame = false;
20                 break;
21             }
22             head--; tail++;
23         }
24         if(isSame){
25             break;
26         }
27     }
28
29     for(var k = s.length - 1; k >= tail && k !== 0; k--){
30         prefix += s[k];
31     }
32     return prefix + s;
33 };
时间: 2024-08-10 19:18:17

[LeetCode][JavaScript]Shortest Palindrome的相关文章

Java for LeetCode 214 Shortest Palindrome

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", return "aaacecaaa&qu

[LeetCode][JavaScript]Valid Palindrome

https://leetcode.com/problems/valid-palindrome/ Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."rac

[LeetCode-JAVA] Shortest Palindrome

题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", return "aaacecaa

[LeetCode][JavaScript]Pascal's Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

leetcode -day13 Valid Palindrome & Triangle & Pascal's Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

leetcode题目:Palindrome Partitioning 和Palindrome Partitioning II

题目一: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",

[LeetCode] 244. Shortest Word Distance II 最短单词距离 II

This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which receives a list

[LeetCode] 243. Shortest Word Distance 最短单词距离

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. G

[LeetCode][JavaScript]Palindrome Linked List

Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? https://leetcode.com/problems/palindrome-linked-list/ 判断单链表是否为回文,要求时间复杂度O(n),空间复杂度O(1). 如果对空间复杂度没有要求,有两种简单的做法.