[Leetcode] [JavaScript] Shortest palindrome

Source: Internet
Author: User

Shortest palindrome

Given A string S, you is 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/

There is no use of the noble KMP algorithm.

It looks like the complexity of O (n^2), but the final effect is quite good, 140ms, and in many cases can break off.

The main idea is to take a point, that is, the desired palindrome center, head and tail point to it, first find the same number, adjust the pointer position, and then head--, tail++ to retrieve the text.

If the head equals 0, the description is found, and the character appended to the output tail and the input string is the answer.

Then this palindrome center of the problem, the middle of the string and the middle before the point can be a palindrome center, the latter half can be ruled out.

Palindrome Center may be single or multiple letters, if it is more than one letter, they must be the same.

Lift chestnuts: "DABBBBBAAC", h means head,t represents tail

1. A A b b b b b A A C--start all pointing to the middle

HT

2. A A b b b b b A A C--find the same element around

H T

3. A A b b b b b A a C--same, continue to find

H T

3. A A b b b b b A A C--the prefix is found, the output c+ the original string

H T

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

The idea of this solution are 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 has the same value, then adjust the pointers.

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

If head is equals to 0 and 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 isn't a single character, they should be same letters.

1 /**2 * @param {string} s3 * @return {string}4  */5 varShortestpalindrome =function(s) {6     varprefix = "";7     varPos, head, tail;8 9       for(pos = head = Tail = parseint (S.LENGTH/2); pos > 0; head = Tail =--POS) {Ten          while(Head!== 0 && s[head-1] = = =S[head]) { Onehead--; pos--; A         } -          while(Tail! = s.length-1 && s[tail + 1] = = =S[tail]) { -tail++; the         } -         varIssame =true; -          while(Head >= 0){ -             if(S[head]!==S[tail]) { +Issame =false; -                  Break; +             } Ahead--; tail++; at         } -         if(issame) { -              Break; -         } -     } -  in      for(varK = s.length-1; K >= Tail && k!== 0; k--){ -Prefix + =S[k]; to     } +     returnPrefix +s; -};

[Leetcode] [JavaScript] Shortest palindrome

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.