# Middle-题目106：5. Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

public class Solution {
public String longestPalindrome(String s) {
char[] ca = s.toCharArray();
int rs = 0, re = 0;
int max = 0;
for(int i = 0; i < ca.length; i++) {
if(isPalindrome(ca, i - max - 1, i)) {
rs = i - max - 1; re = i;
max += 2;
} else if(isPalindrome(ca, i - max, i)) {
rs = i - max; re = i;
max += 1;
}
}
return s.substring(rs, re + 1);
}

private boolean isPalindrome(char[] ca, int s, int e) {
if(s < 0) return false;

while(s < e) {
if(ca[s++] != ca[e--]) return false;
}
return true;
}
}

public class Solution {
public String longestPalindrome(String s) {
if (s==null || s.length()==0)
return "";
char[] s2 = addBoundaries(s.toCharArray());
int[] p = new int[s2.length];
int c = 0, r = 0; // Here the first element in s2 has been processed.
int m = 0, n = 0; // The walking indices to compare if two elements are the same
for (int i = 1; i<s2.length; i++) {
if (i>r) {
p[i] = 0; m = i-1; n = i+1;
} else {
int i2 = c*2-i;
if (p[i2]<(r-i)) {
p[i] = p[i2];
m = -1; // This signals bypassing the while loop below.
} else {
p[i] = r-i;
n = r+1; m = i*2-n;
}
}
while (m>=0 && n<s2.length && s2[m]==s2[n]) {
p[i]++; m--; n++;
}
if ((i+p[i])>r) {
c = i; r = i+p[i];
}
}
int len = 0; c = 0;
for (int i = 1; i<s2.length; i++) {
if (len<p[i]) {
len = p[i]; c = i;
}
}
char[] ss = Arrays.copyOfRange(s2, c-len, c+len+1);
return String.valueOf(removeBoundaries(ss));
}
private  char[] addBoundaries(char[] cs) {
if (cs==null || cs.length==0)
return "||".toCharArray();

char[] cs2 = new char[cs.length*2+1];
for (int i = 0; i<(cs2.length-1); i = i+2) {
cs2[i] = '|';
cs2[i+1] = cs[i/2];
}
cs2[cs2.length-1] = '|';
return cs2;
}
private  char[] removeBoundaries(char[] cs) {
if (cs==null || cs.length<3)
return "".toCharArray();

char[] cs2 = new char[(cs.length-1)/2];
for (int i = 0; i<cs2.length; i++) {
cs2[i] = cs[i*2+1];
}
return cs2;
}
}

cmershen的碎碎念：
Manacher, Glenn (1975), “A new linear-time “on-line” algorithm for finding the smallest initial palindrome of a string”, Journal of the ACM 22 (3): 346–351, doi:10.1145/321892.321896.

• 本文已收录于以下专栏：

## LeetCode(5) Longest Palindromic Substring

• feliciafay
• 2013年11月27日 14:00
• 17814

## LeetCode 5 Longest Palindromic Substring(C,C++,Python,Java)

Problem: Given a string S, find the longest palindromic substring in S. You may assume that the ma...
• runningtortoises
• 2015年05月06日 15:38
• 1347

## LeetCode-5-Longest Palindromic Substring(动态规划)-Medium

• eddy_liu
• 2015年12月20日 16:49
• 363

## LeetCode5——Longest Palindromic Substring 最易懂的解法

Given a string S, find the longest palindromic substring in S. You may assume that the maximum l...
• booirror
• 2016年01月22日 23:05
• 910

## Leetcode 5. Longest Palindromic Substring 新补充Manacher算法

5. Longest Palindromic Substring Total Accepted: 98755 Total Submissions: 434700 Difficulty: Medi...
• fantasiasango
• 2016年03月15日 09:34
• 524

## LeetCode 5:Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum len...
• sunao2002002
• 2015年05月12日 00:38
• 2637

## [LeetCode]5 Longest Palindromic Substring(C++，Python实现)

LeetCode OJ的第五题，如果有问题或者给我指点欢迎来信讨论ms08.shiroh@gmail.com 题目描述...
• Shiroh_ms08
• 2014年05月23日 21:28
• 3111

## LeetCode --- 5. Longest Palindromic Substring

• makuiyu
• 2015年01月27日 22:25
• 797

## Java Longest Palindromic Substring(最长回文字符串)

• soszou
• 2014年07月06日 10:38
• 16279

## Longest Palindromic Substring -- LeetCode

• linhuanmars
• 2014年03月10日 03:49
• 20081

举报原因： 您举报文章：Middle-题目106：5. Longest Palindromic Substring 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)