Leetcode #5 longest palindromic Substring (M)

Source: Internet
Author: User

[Problem]

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.

[Analysis]

There are many ways of thinking about this problem, and there are various discussions on the Internet. What I'm using here is someone who feels better. A method using dynamic programming is used to solve the problem, and the time and space complexity are O (n^2). DP[I][J] is true when the substring from I to J is recorded as Palindromic,base case is I >= J. Then, for each location, whether the substring from the I-start length k is palindromic.

For S[I...J] There are two things:

1. s[i]! = s[j], then dp[i][j] = false;

2. s[i] = = S[j], then dp[i][j] = dp[i + 1][j-1]

Note that in the process of nested loop solving, K outer loop, that is, each loop is actually to solve the K-length sub-string for each position is palindromic, so as to ensure that the solution Dp[i][j] Dp[i + 1][j-1] has a solution.

Another simple idea is to calculate the length of the longest back text string centered on each possible center point. This center point has N-1 + N = 2N, and the time complexity is also O (n^2). Manacher's algorithm can get the optimal time complexity O (n), but it is difficult to understand.

Reference:

Leetcode (5) Longest palindromic substring:http://blog.csdn.net/feliciafay/article/details/16984031

Longest palindromic Substring part ii:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

[Solution]

 Public classSolution { PublicString Longestpalindrome (String s)//Dynamic Programming Solution    {          if(s.length () = = 0)          {              return""; }          if(s.length () = = 1)          {              returns; }            /** DP denotes if a substring s[i: j] is a palindromic*/        Boolean[] DP =New Boolean[S.length ()][s.length ()]; inti,j; /** Initialize DP*/         for(i = 0; i < s.length (); i++)          {               for(j = 0; J < S.length (); j + +)              {                  if(I >=j) {/** if i = = J, it is a palindromic substring of length 1;                        * If I > J, regard it as an empty substring when initializing. */Dp[i][j]=true; }                  Else                  {                      /** Otherwise, initialize to False*/Dp[i][j]=false; }              }          }                    intK; intMaxLen = 1; intStart = 0, end = 0;  for(k = 1; k < s.length (); k++)          {               /** Test If a substring starts from s[i] of length k was palindromic * Note that we have the B ASE case when k = 1 It 's always palindromic*/             for(i = 0; k + i < s.length (); i++) {J= i +K; if(S.charat (i)! =S.charat (j)) {Dp[i][j]=false; }                  Else{Dp[i][j]= Dp[i+1][j-1]; if(Dp[i][j]) {if(k + 1 >maxlen) {MaxLen= k + 1; Start=i; End=J; }                      }                  }              }          }          returnS.substring (Start, end + 1); }}

Leetcode #5 longest palindromic Substring (M)

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.