[Leetcode] Longest palindromic substring solution report

Source: Internet
Author: User

DP and KMP are too tall. I thought of a Simple Traversal method.

[Question]

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

[Idea] (should be O (n)

Search from the middle to the two ends and find the longest response substring centered on each letter. If the remaining elements on both sides are shorter than half of the current longest response substring, stop traversing.

Don't look at the code for easy understanding.

[Java code]

Public class solution {Public String longestpalindrome (string s) {int Len = S. length (); If (S = NULL | Len = 1) {return s;} string ret = ""; int mid = Len/2; int I = 0; while (true) {// when traversing to both ends of S, if the longest response centered on Mid + I or mid-I is no longer than the current optimal solution, traversal ends if (2 * (LEN-(Mid + I) <ret. length () & 2 * (mid-I + 1) <ret. length () {break;} string str1 = palindrome1 (S, Mid + I); string str2 = palindrome2 (S, Mid + I); string str3 = Palindrome1 (S, mid-I); string str4 = palindrome2 (S, mid-I); If (str1.length ()> ret. length () {ret = str1;} If (str2.length ()> ret. length () {ret = str2;} If (str3.length ()> ret. length () {ret = str3;} If (str4.length ()> ret. length () {ret = str4;} I ++;} return ret ;} // find out the ABA-type echo substring in S centered on index Public String palindrome1 (string S, int index) {string ret = ""; int I = index, j = index; While (I> = 0 & J <S. Length () {If (S. charat (I )! = S. charat (j) {break;} ret = S. substring (I, j + 1); I --; j ++;} return ret ;} // find out the abba-type echo substring in S centered on index and index + 1 Public String palindrome2 (string S, int index) {string ret = ""; int I = index, j = index + 1; while (I> = 0 & J <S. length () {If (S. charat (I )! = S. charat (j) {break;} ret = S. substring (I, j + 1); I --; j ++;} return ret ;}}

[Analysis]

Later, I found a similar solution on the Internet. For example, http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html, in order not to distinguish ABA and ABBA types of return substrings, constructs a new string T, insert a special character '#' between the two ends and each two letters '#', in this way, when the string is centered on '#', it is the ABBA type substring in my code.

In comparison to the code, I also use a small trick, that is, traversing from the center to the two ends, so if there is a long echo substring in the middle, therefore, when the two ends are relatively biased, the judgment can be omitted.

Once again, I got AC. Later I compared the online solution, so I was a little excited ~.

The analysis may be incorrect. You may feel good about yourself. please correct me!


[Leetcode] Longest palindromic substring solution report

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.