/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
if (str.length() <= 1)
return str;
for (int i = 1; i < str.length(); i++) {
// Consider odd length
int low = i;
int high = i;
while(str.charAt(low) == str.charAt(high)) {
low--;
high++;
if (low == -1 || high == str.length())
break;
}
String palindrome
= str.
substring(low
+1, high
); if (palindrome.length() > LPS.length()) {
LPS = palindrome;
}
// Consider even length
low = i-1;
high = i;
while(str.charAt(low) == str.charAt(high)) {
low--;
high++;
if (low == -1 || high == str.length())
break;
}
palindrome = str.substring(low+1, high);
if (palindrome.length() > LPS.length()) {
LPS = palindrome;
}
}
return LPS;
}
{
// your code goes here
Ideone x = new Ideone();
System.
out.
println(x.
longestPalindrome("aaebabad")); System.
out.
println(x.
longestPalindrome("cbeereed")); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCVN0cmluZyBsb25nZXN0UGFsaW5kcm9tZShTdHJpbmcgc3RyKSB7CgogICAgaWYgKHN0ci5sZW5ndGgoKSA8PSAxKQogICAgICByZXR1cm4gc3RyOwoKICAgIFN0cmluZyBMUFMgPSAiIjsKCiAgICBmb3IgKGludCBpID0gMTsgaSA8IHN0ci5sZW5ndGgoKTsgaSsrKSB7CgogICAgICAvLyBDb25zaWRlciBvZGQgbGVuZ3RoCiAgICAgIGludCBsb3cgPSBpOwogICAgICBpbnQgaGlnaCA9IGk7CiAgICAgIHdoaWxlKHN0ci5jaGFyQXQobG93KSA9PSBzdHIuY2hhckF0KGhpZ2gpKSB7C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