const longestPalindrome = (s) => {
if(s === s.split('').reverse().join('')) return s;
let charArray = [],
countArray = [];
let currentWord = s[0];
let count = 1;
let sLength = s.length+1;
for(let i=1; i<sLength; i++) {
if(s[i] === s[i-1]) {
count++;
currentWord += s[i];
} else {
countArray.push(count);
charArray.push(currentWord);
currentWord = s[i];
count = 1;
}
}
let head = 0;
let tail = 0;
let maxWord = '';
let charLength = charArray.length;
for(let k=0; k<charLength; k++){
currentWord = charArray[k];
tail = head + countArray[k] - 1;
let steps = sLength-countArray[k];
for(let l=1; l<steps; l++){
if(s[head-l] === s[tail+l]) {
currentWord = s[head-l] + currentWord + s[tail+l];
} else {
maxWord = maxWord.length < currentWord.length ? currentWord : maxWord;
head = tail+1;
break;
}
}
}
return maxWord;
}