# LeetCode 5. Longest Palindromic Substring

Total Accepted: 200641
Total Submissions: 798974
Difficulty: Medium
Contributor: LeetCode
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

# 分析

## 方法一：最长公共子串

public class Solution {
/**
* @param s input string
* @return the longest palindromic substring
*/
public String longestCommonSubstring(String A, String B) {
int n = A.length();
int m = B.length();

int[][] dp = new int[n+1][m+1];
//dp[0][0] = 0;

for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(A.charAt(i-1) == B.charAt(j-1))
dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = 0;
}
}
int x = 0,y = 0;
int max = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if(dp[i][j] > max) {
max = dp[i][j];
x = i-1;
y = j-1;
}
}
}

StringBuilder sb =new StringBuilder();
while(x >=0 && y>=0) {
if(A.charAt(x) == B.charAt(y))
{sb.append(A.charAt(x));
x--;
y--;}
else
break;
}

return sb.reverse().toString();
}

public String longestPalindrome(String s) {
if(s.length() == 0)
return "";

String sr = reverseStr(s);

return longestCommonSubstring(s, sr);

}

public String reverseStr(String s) {
StringBuilder sb = new StringBuilder(s);
sb.reverse();
return sb.toString();
}

}

## 方法二：动态规划

dp[i][j]:表示i到j的字符串，是不是回文串，是就为true，不是就为false

public String longestPalindrome(String s) {
int n = s.length();
String res = null;

boolean[][] dp = new boolean[n][n];

for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
dp[i][j] = s.charAt(i) == s.charAt(j) && (j - i < 3 || dp[i + 1][j - 1]);

if (dp[i][j] && (res == null || j - i + 1 > res.length())) {
res = s.substring(i, j + 1);
}
}
}

return res;
}

## 方法三：两边扩展法

public String longestPalindrome(String s) {
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}

private int expandAroundCenter(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
return R - L - 1;
}