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)