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.

If a string is written from left to right and right-to-left, such a string is called a palindromic string, such as ABA, or ABBA. This is the case, given the input of a string, requires the output of a substring, so that the substring is the longest padromic string.

1  Public classSolution {2      Publicstring Longestpalindrome (string s) {3String longets = "";4         if(S.length () <= 1) {5             returns;6         }7         Boolean[] IsP =New Boolean[S.length ()][s.length ()];8          for(inti = 0; I < s.length (); i++) {9Isp[i][i] =true;Ten         } One          for(inti = 0; I < S.length ()-1; i++) { A             if(S.charat (i) = = S.charat (i + 1)) { -Isp[i][i + 1] =true; -Longets = s.substring (i, i + 2); the             } -         } -  -          for(intL = 3; L <= s.length (); l++) { +              for(inti = 0; I <= s.length ()-l; i++) { -                 intj = i + l-1; +                 if(S.charat (i) = =S.charat (j)) { AISP[I][J] = isp[i + 1][j-1]; at                     if(Isp[i][j] && l >longets.length ()) { -Longets = S.substring (i, j + 1); -                     } -                 } -             } -         } in         returnlongets; -  to     } +  -  the}

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.