Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example:
Input: "cbbd" Output: "bb"
public class Solution {
public String longestPalindrome(String s) {
if (s.length() == 0) {
return s;
}
String result = "";
for (int i = 0; i < s.length(); i++) {
String temp = isPalidrome(s, i, i);
if (temp.length() > result.length()) {
result = temp;
}
temp = isPalidrome(s, i-1, i);
if (temp.length() > result.length()) {
result = temp;
}
}
return result;
}
public String isPalidrome(String s, int start, int end) {
if (start < 0 || end >= s.length()) {
return "";
}
while (start >= 0 && end < s.length()) {
if (s.charAt(start) == s.charAt(end)) {
start--;
end++;
} else {
break;
}
}
return s.substring(start+1, end);
}