Leetcode05-longest palindromic substring Java version

Source: Internet
Author: User

My Leetcode journey, this chapter mainly completes the use of Java implementation algorithm. This is the 5th article longest palindromic Substring


All code Download: GitHub Link: GitHub link, click Surprise, write article is not easy, welcome everyone to take my article, and give useful comments, of course you can also pay attention to my GitHub;

1. Topic Description:

Given a string S, find the longest palindromic substring in s. The maximum length of S is assume, and there exists one unique longest palindromic substring.

2. My train of thought:

1. The longest palindrome string solution
2. Once you see the longest palindrome string, I think about it. Manacher algorithm
3. Algorithmic thinking: See http://blog.sina.com.cn/s/blog_3fe961ae0101iwc2.html

3. My AC Code
 Packagecom.rlovep.string;/** * Longest palindromic Substring * My idea: * 1. Longest palindrome string solution * See the longest palindrome string I thought of the manacher algorithm * algorithm idea: Please see my reproduced blog * @author  Peace * * * * Public  class palindromicsubstring {      PublicStringLongestpalindrome(String input) {intpos=0;Center position of the right palindrome substring                intmaxright=0;right-most position of the right-most palindrome substring                intMaxlength=0;//String maximum palindrome length                intfinpos=0;intfinmaxright=0;//Add delimiterStringBuilder sb=NewStringBuilder (); Sb.append ("#"); for(intI=0; I<input.length (); i++) {Sb.append (Input.charat (i)); Sb.append ("#"); }//Get a string arrayInput=sb.tostring ();Char[] s= Input.tochararray ();intRl[] =New int[S.length];//Store maximum palindrome string radius for each location                 for(intI=0; i<s.length;i++) {if(I<maxright) {Rl[i]=math.min (rl[2*pos-i], maxright-i);//Core code}Else{rl[i]=1; } while((I-rl[i]) >=0&&//cannot exceed left side(I+rl[i]) <s.length&&//cannot exceed right side(S[i-rl[i]]==s[i+rl[i])) {rl[i]+=1; }//Update the right-most position of the right-most palindrome substring                    if((i+rl[i]-1) >maxright) {pos=i; maxright=i+rl[i]-1; }if(rl[i]>maxlength)                        {finpos=i;                        Finmaxright=maxright;                    Maxlength=rl[i]; }} input=input.substring (finpos+1-maxlength,finmaxright);returnInput.replace ("#",""); }}

Ok this chapter is introduced here from the Wpeace (blog.wpeace.cn)

Leetcode05-longest palindromic substring Java version

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.