Leetcode 005 Longest palindromic Substring (Java)

Source: Internet
Author: User

5. Longest palindromic substringmy SubmissionsQuestionTotal accepted:87802 Total submissions:399054 difficulty:medium

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.

Subscribe to see which companies asked this question

Puzzle: Find the maximum palindrome string

Each letter is centered and swept on both sides. Let's go again.

Import Java.awt.print.printable;public class Solution {public static string Longestpalindrome (string s) {string Longestpalindrome=null; String tmp=null;int maxlen=0;if (s.length () ==1) return s;for (int i=0;i<s.length (); i++) {Tmp=getpalindrome (s, I, I); if (Maxlen<tmp.length ()) {longestpalindrome=tmp;maxlen=tmp.length ();} Tmp=getpalindrome (S, i-1, i); if (Maxlen<tmp.length ()) {longestpalindrome=tmp;maxlen=tmp.length ();}} return longestpalindrome;    } public static string Getpalindrome (String s,int Begin,int end) {while (Begin>=0&&end<s.length () & &s.charat (BEGIN) ==s.charat (end) {begin--;end++;} Return s.substring (begin+1, end);} public static void Main (string[] args) {System.out.println (Longestpalindrome ("BB"));}}

  

Leetcode 005 Longest palindromic Substring (Java)

Related Article

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.