Longest palindrome substring

Source: Internet
Author: User

The longest palindrome string generally has the following two conditions:

problem Description 1: Given a string, the length of the longest palindrome substring is obtained, and the characters of the palindrome substring must be contiguous in the original string.

Analysis: It is obvious that the brute force method can be used to solve the problem by enumerating all the substrings and judging whether they are palindrome. But this method is inefficient, and time complexity is too high, and if a long substring contains another short substring, then the palindrome judgment of the substring is not needed, and so on.

We can optimize it, because palindrome string is centered around the character is symmetrical, such as palindrome string "ABA", the center of B, its left and right side symmetry, are a.

So we can enumerate the center position, extend from the center to both sides, record and update the longest palindrome length. It is important to note that the length of the palindrome is counted or even, so it needs to be discussed separately.

The specific Java code below, the statements are very common, the reader can easily convert to other languages:

1 ImportJava.util.*;2  classTest {3        Public Static intmaxhuiwenlength (StringBuilder str) {4           if(Str.length () ==0)return0;//If the length is an empty string, return 0 directly5           intI,j,max=0,p;//Max records the longest length6           intN=str.length ();//To find the string length7            for(i=0;i<n;i++)8           {9                for(j=0; (i-j) >=0 && (i+j) <n;j++)//assuming an odd length of palindrome stringTen                   if(Str.charat (I-J)!=str.charat (i+j)) Break; Onep= (j-1) *2+1; A               if(P>max) max=p; -                for(j=0; (i-j) >=0 && (i+j+1) <n;j++)//assuming an even number of palindrome strings -                   if(Str.charat (I-J)!=str.charat (i+j+1)) Break; thep= (j-1) *2+2; -               if(P>max) max=p; -           }   -           returnMax//return palindrome string length +       }         -     } +   A  Public classMain { at      Public Static voidMain (string[] args) { -StringBuilder str=NewStringBuilder ("123458");  -System.out.print ("string:" +str+ "the maximum palindrome substring length is:" +test.maxhuiwenlength (str)); -     } -  -}
View Code

Problem Description 2: Given a string, the length of the longest palindrome substring is obtained, and the characters of the palindrome substring can be discontinuous in the original string. This means that you can delete one or more characters from the original string.

Analysis: This problem compared to problem 1, with a certain degree of difficulty, the difficulty is that you can delete the characters in the original string, but the basic idea is the same as problem 1, you need to do some improvement on problem 1.

It is also necessary to consider whether palindrome strings are odd or even, regardless of the case, follow the process.

When scanning to the end of the word typeface, and so on, both sides to expand, if not equal, it will be divided into two steps:

The first step, fixed the right end, scan to the left, if found equal, the two ends are expanded simultaneously, the length plus 2. Otherwise left hands-1

The second step, fixed left side, scan to the right, if found equal, the two ends are expanded simultaneously, the length plus 2. Otherwise right pointer +1

The specific Java code is as follows, the code is more generic, the reader can easily be converted to other languages.

1 ImportJava.util.*;2  classTest {3        Public Static intmaxhuiwenlength (StringBuilder str) {4           if(Str.length () ==0)return0;//If the length is an empty string, return 0 directly5           intI,j,max=0,p;//Max records the longest length, p records the temporary maximum length6           intN=str.length ();//To find the string length7           intLow,high;//two pointers pointing to each end8            for(i=1;i<n;i++)9           {   TenP=1;//find the odd-length palindrome centered on I, with a minimum length of 1 OneLow=i-1;high=i+1; A                while(Low>=0 && high<N) -               { -                   if(Str.charat (Low) ==str.charat (high))//if both ends are equal, both ends are expanded at the same time, and the length is 2 the                       { -low--; -high++; -P=p+2;  +                       } -                   Else{//If the ends are not equal +                       intt=Low ; A                        while(--low>=0 && high<n)//right end fixed, scan left at                           if(Str.charat (Low) ==str.charat (high))//when scanning to equality, both sides expand at the same time, the length plus 2 -                           { -high++; -P=p+2; -                           } -low=T; in                        while(Low>=0 && ++high<n)//left end fixed, scan right -                           if(Str.charat (Low) ==str.charat (high))//when scanning to equality, both sides expand at the same time, the length plus 2 to                           { +low--; -P=p+2; the                           } *                   } $               }Panax Notoginseng               if(P>max) max=p;//Update Maximum length -                theP=0;//looking for even-length palindrome with I-1,i as the center +Low=i-1;high=i; A                while(Low>=0 && high<N) the               { +                   if(Str.charat (Low) ==str.charat (high))//if both ends are equal, both ends are expanded at the same time, and the length is 2 -                       { $low--; $high++; -P=p+2; -                       } the                   Else{ -                       intt=Low ;Wuyi                        while(--low>=0 && high<n)//right end fixed, scan left the                           if(Str.charat (Low) ==str.charat (high))//when scanning to equality, both sides expand at the same time, the length plus 2 -                           { Wuhigh++; -P=p+2; About                           } $low=T; -                        while(Low>=0 && ++high<n)//left end fixed, scan right -                           if(Str.charat (Low) ==str.charat (high))//when scanning to equality, both sides expand at the same time, the length plus 2 -                           { Alow--; +P=p+2; the                           } -                   } $               } the               if(P>max) max=p;//Update Maximum length the           }   the           if(max==0) Max=1;//because as long as the string is not empty, the palindrome string is at least 1. the           returnMax//return palindrome string length -       }         in     } the   the  Public classMain { About      Public Static voidMain (string[] args) { theStringBuilder str=NewStringBuilder ("123251");  theSystem.out.print ("string:" +str+ "the maximum palindrome substring length is:" +test.maxhuiwenlength (str)); the     } +  -}
View Code

Longest palindrome substring

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.