• [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 };
  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/Liok3187/p/4525131.html
Copyright © 2020-2023  润新知