LeetCode5 Longest palindromic Substring

Source: Internet
Author: User

Describe:Given a stringS, find the longest palindromic substring inS. Assume that maximum length of s  is, and there exists one unique longest palindromic substring.

Give a string string to find the largest string of strings. This is a typical longest palindrome string problem, there are currently four kinds of solutions.

1. Violence test, test each substring, obviously this is the dumbest method, Time complexity O (n^3)

2. Dynamic planning, refer to http://blog.163.com/zhaohai_1988/blog/static/2095100852012716105847112/and http://www.cnblogs.com/ en-heng/p/3973679.html

3. Center extension method, which is also the first thing I think of when I see the topic, with an element as the center, respectively, to expand forward and backward, to see whether the same, to consider abcba form abccba form the time complexity of these two different palindrome substrings is O (n^2).

1  Public StaticString Matchchar (String s,intLintR) {//match characters with L and R as subscript2         intleft=l;3         intright=R;4          while(Left<=right&&left>=0&&right<s.length () &&s.charat (left) = =S.charat (right)) {5left--;6right++;7         }8         returnS.substring (left+1, right);9     }Ten      Publicstring Longestpalindrome (string s) { One         intn=s.length (); A         if(0==n)return""; -String longeststring=s.substring (0,1);//a character -          for(inti=0;i<n-1;i++){ theString Tempstring1=matchchar (S, I, I);//ABCBA Form -             if(Tempstring1.length () >longeststring.length ()) -longeststring=tempString1; -String Tempstring2=matchchar (s,i,i+1);//ABCCBA Form +             if(Tempstring2.length () >longeststring.length ()) -longeststring=tempString2; +         } A         returnlongeststring; at}

4. The legendary Manacher algorithm, time complexity O (n), is also a more difficult to understand algorithm. Read a few blog, want to Know can refer
http://blog.csdn.net/ggggiqnypgjg/article/details/6645824
Http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

LeetCode5 Longest palindromic 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.