Programming method: Interview and algorithm experience (longest palindrome)

Source: Internet
Author: User

All the content comes from the programming method: Interview and algorithm experience book, the implementation is written by themselves using Java

Title Description

Given a string, find the length of its longest palindrome substring.

Analysis and Solution

The easiest way to do this is to enumerate all the substrings and determine whether they are palindrome. This idea seems to be correct at first, but it does a lot of useless work, if a long substring contains another short substring, then the palindrome judgment of the substring is not necessary.

Solution One

So how to make judgments efficiently? We think that if a string is a palindrome, then a character-centric prefix and suffix are the same, for example, a palindrome string "ABA" for example, with the center of B, its prefix and suffix are the same, is a.

So, can we enumerate the central location and then use the extension method at that location to record and update the longest palindrome length we get? The answer is yes, the reference code is as follows:

/** Enumerate the center position, and then in that position with the extension, record and update the resulting maximum palindrome length*/     Public Static intlongestPalindrome1 (String s) {intEvenmaxlength=0; intOddmaxlength = 0; //an odd length         for(intI=0;i<s.length (); i++)        {            intJ=i-1; intK=i+1; intTemp=1;  while(J>=0&&k<s.length () &&s.charat (j) = =S.charat (k)) {Temp= temp+2; J--; K++; } oddmaxlength= Oddmaxlength>temp?oddmaxlength:temp; }        //length is even         for(intI=0;i<s.length (); i++)        {            intj=i; intK=i+1; intTemp=0;  while(J>=0&&k<s.length () &&s.charat (j) = =S.charat (k)) {Temp= temp+2; J--; K++; } evenmaxlength= Evenmaxlength>temp?evenmaxlength:temp; }        returnOddmaxlength>evenmaxlength?oddmaxlength:evenmaxlength; }
Solution of two, O (N) solution

In the above solution one: enumerating the central position, we need to specifically consider whether the length of the string is odd or even, so that we write code implementation of the odd and even when the case is written separately, is there a way to do without tube length is odd or even, and unified processing it? For example, can I convert all the cases to odd processing?

The answer is yes. This is the manacher algorithm we are going to see below, and the time complexity of this algorithm for the longest palindrome string is linear O (N).

First, by inserting a special symbol on both sides of each character, all possible odd or even lengths of palindrome substrings are converted to odd lengths. For example, Abba becomes #a #b#b#a#, ABA becomes #a #b#a#.

/** Add special characters to the inside. Added "#" to make Abba into a#b#b#a*/     Public Static intlongestPalindrome2 (String s) {stringbuffer S1=NewStringBuffer (s); intLength=s1.length ();  for(inti=0,k=1;i<length-1;i++)//Add # to a string{S1.insert (k,"#"); K=k+2; } s=s1.tostring (); String Temp= ""; intMax = 0;  for(intI=0;i<s.length (); i++)        {            intJ=i-1; intK=i+1; intMaxtemp = 1; String maxstring=character.tostring (S.charat (i));  while(J>=0&&k<s.length () &&s.charat (j) = =S.charat (k)) {Maxtemp= maxtemp+2; Maxstring= Character.tostring (S.charat (j)) +maxstring+character.tostring (S.charat (k)); J--; K++; }            if(max<maxtemp) {Max=maxtemp; Temp=maxstring; }} Temp= Temp.replaceall ("#", "" "); returntemp.length (); }

Programming method: Interview and algorithm experience (longest palindrome)

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.