var longestPalindrome = function(s) {
var len = s.length;
if(len == 1){
return s;
}else if(len == 2){
return s[0] === s[1] ? s : s[0];
}
var result = '';
var startindex = 0 ,endindex = 2;
for(var i = 2; i < len; i++){
if(s[i] != s[i - 1] && s[i] != s.charAt(startindex)){
if(i == len - 1){
if(s[i - 1] == s[i - 2]){
if(i > 2){
s.substring(startindex + 1 ,i).length > result.length ? result = s.substring(startindex + 1 ,i) : '';
}else{
s.substring(0 ,2).length > result.length ? result = s.substring(0 ,2) : '';
}
}else{
s[0].length > result.length ? result = s[0] : '';
}
}else{
if(s[i - 1] == s[i - 2]){
s.substring(startindex ,i).length > result.length ? result = s.substring(startindex ,i) : '';
startindex = i - 1;
}else{
startindex ++;
}
endindex ++;
}
}else if(s[i] == s[i - 1] && s[i] == s.charAt(startindex)){
if(i == len - 1){
s.length > result.length ? result = s : '';
}else{
endindex ++;
}
}else if(s[i] != s[i - 1] && s[i] == s.charAt(startindex)){
s.substring(startindex ,endindex + 1).length > result.length ? result = s.substring(startindex ,endindex + 1) : '';
startindex = i - 1;
endindex ++;
}else if(s[i] == s[i - 1] && s[i] != s.charAt(startindex)){
if(i == len - 1){
s.substring(startindex + 1 ,i + 1).length > result.length ? result = s.substring(startindex + 1 ,i + 1) : '';
}else{
endindex ++;
}
}
}
return result;
};
console.log(longestPalindrome('abcba'));
/*
cbbd
qfv
ddd
dd
cdc
mgmgqcccccccqbccccbxoooooooooooxo
a
ab
dcccd
yyyy1yyyyyy
*/
console