
Manacher's Algorithm – Longest Palindromic Substring - nsnick
http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
======
rawnlq
A slower O(nlog(n)) but IMO much simpler algorithm is to just using a rolling
hash[1]. With linear time precomputation you can compute the hash of any
substring range in constant time.

Then for each position, you can just binary search for the longest palindrome
centered at that position.

[1][http://en.wikipedia.org/wiki/Rolling_hash](http://en.wikipedia.org/wiki/Rolling_hash)

------
btilly
If you don't get the explanation in the article, one of the multiple alternate
explanations or corrections at
[http://stackoverflow.com/questions/10468208/manachers-
algori...](http://stackoverflow.com/questions/10468208/manachers-algorithm-
algorithm-to-find-longest-palindrome-substring-in-linear-t) might suffice
instead.

------
anon4
People, please. This presents a problem poignantly and provides a pleasant
programmatic solution - the precise point of "hacker news". Be not so butt-
bothered about non-apparent algorithm applications.

~~~
JadeNB
Based on time-stamps, at the time you posted there were 4 comments, of which 2
complained about the lack of applications and 2 engaged seriously with the
material. That seems too early in the discussion to be accusing the community
(as opposed to 2 individual posters) of being "butt-bothered".

On the other hand, there were no further such comments after your post, so
maybe one could say that it worked. :-)

~~~
anon4
I did it for the alliterative appeal.

~~~
JadeNB
Ah; I noticed all the 'p's, but not all the 'b's. (I did notice the two 'a's
in the response. :-) )

------
jhdevos
Wording is a bit sloppy: "Extending R (the inner while loop) takes at most a
total of N steps, and positioning and testing each centers take a total of N
steps too. Therefore, this algorithm guarantees to finish in at most 2*N
steps, giving a linear time solution."

What he means is that the total time taken by ALL executions of the inner loop
combined is at most N, not that each run of the inner loop takes at most N
steps (otherwise it would become quadratic).

------
imslavko
If you like this, Manacher's algo is very similar to z-string matching algo:
[http://codeforces.com/blog/entry/3107](http://codeforces.com/blog/entry/3107)

------
vishalzone2002
its funny its on HN. I just got asked this question in an tech interview last
week..

------
amelius
Are there any applications for this?

~~~
berodam
I've seen it used in an application that manages DNA plasmid data, it could be
useful in a lot of genomic analysis tools.

------
wmkn
Finding palindromes in C strings. The most important problem in computer
science. If software engineering job interviews are to be believed.

