[Leetcode] Longest palindromic Substring

Source: Internet
Author: User

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.

Thinking of solving problems

Online there is a way of thinking is to get the input string of the inverse string, and then use dynamic programming to get the oldest string of these two strings, but in fact there is a problem. For example, for input "ABCDEFDCBA", it will get an error in the palindrome substring "ABCD".
A correct idea is to use a two-dimensional array to record whether or not a palindrome string is between each of the two subscripts. Subscript I moves forward from the last node of the string, subscript J moves backwards from I, if the subscript I and J point to the same characters, and j-i<=2 or [i+1][j-1] between the sub-string is a palindrome substring, then [i][j] the sub-string between a palindrome substring.

Implementation code

A code that has a problem:

Const intLEN =1001;intC[len][len];class Solution { Public:string Longestpalindrome(strings) {memset (c,0,sizeof(c));stringS2 ="";inti = s.size ()-1; while(I >=0) {s2 + = s[i--]; }intMaxLen =0;intMaxI =0; for(i =1; I <= s.size (); i++) { for(intj =1; J <= S2.size (); J + +) {if(s[i-1] = = s2[j-1]) {C[i][j] = c[i-1][j-1] +1;if(C[i][j] > MaxLen)                        {maxlen = c[i][j]; MaxI = i-1; }                }            }        }returnS.SUBSTR (Maxi-maxlen +1, MaxLen); }};

Correct code (c + +):

//runtime:260 MSConst intLEN = +;BOOLC[len][len];class Solution { Public:string Longestpalindrome(strings) {memset (c,0,sizeof(c));intMaxLen =0;stringTemp for(inti = s.size ()-1; I >=0; i--) { for(intj = i; J < S.size (); J + +) {if(S[i] = = S[j] && (j-i <=2|| c[i+1][j-1]) {C[i][j] =true;if(J-i +1> maxlen) {maxlen = J-i +1; temp = S.substr (i, J-i +1); }                }            }        }returnTemp }};

Java:

//runtime:460 MS Public  class solution {     PublicStringLongestpalindrome(String s) {BooleanB[][] =New Boolean[S.length ()] [S.length ()];intMaxLen =0; String temp =""; for(inti = s.length ()-1; I >=0; i--) { for(intj = i; J < S.length (); J + +) {if(S.charat (i) = = S.charat (j) && (J-i <=2|| b[i+1][j-1]) {B[i][j] =true;if(J-i +1> maxlen) {maxlen = J-i +1; temp = S.substring (i, J +1); }                }            }        }returnTemp }}

Python version is as follows, but write out to time out, temporarily did not find other good way, please advise.

 class solution:    # @param {string} s    # @return {string}     def longestpalindrome(self, s):b = [[False] * Len (s) forIinchRange (len (s))] result ="'MaxLen =0         forIinchRange (len (s),0, -1): forJinchRange (I, Len (s)):ifS[i] = = S[j] and(J-i <=2 orb[i+1][j-1]): b[i][j] =True                    ifJ-i +1> Maxlen:maxlen = j-i +1result = s[i:j+1]returnResult

[Leetcode] 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.