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) {
int start = 0;
int maxLength = 1;
String res;
if(s.length()==1)
{return s;}
int low=0, high=0;
for (int i = 1; i < s.length(); ++i)
{
low = i - 1;
high = i;
while (low >= 0 && high < s.length() && s.charAt(low) == s.charAt(high))
{
if (high - low + 1 > maxLength)
{
start = low;
maxLength = high - low + 1;
}
--low;
++high;
}
low = i - 1;
high = i + 1;
while (low >= 0 && high <s.length() && s.charAt(low) == s.charAt(high))
{
if (high - low + 1 > maxLength)
{
start = low;
maxLength = high - low + 1;
}
--low;
++high;
}
}
return s.substring(start,start+maxLength);
}
}