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.
Use DP to record whether I to J substring is a palindrome.
DP[I][J] = dp[i + 1][j-1] && (J-i < 2 | | c[i] = = C[j])
public class Solution {
public string Longestpalindrome (string s) {
Important:please reset any member data declared, as
The same solution instance would be a reused for each test case.
int start = 0;
int end = 0;
int length = 0;
int strlength = S.length ();
Boolean[][] dp = new Boolean[strlength][strlength];
for (int j = 0; J < Strlength; J + +) {
for (int i = 0; I <= J; i++) {
char a = S.charat (i);
Char B = S.charat (j);
if (A = = B && ((J-i < 2) | | dp[i + 1][j-1]) {
DP[I][J] = true;
if (length < J-i + 1) {
Length = j-i + 1;
start = i;
end = J;
}
}
}
}
Return s.substring (Start, end + 1);
}
}
5. Longest palindromic Substring