Solución
solution.tsTypeScript
function isPalindrome(s: string): boolean {
if (s.length < 1) return false;
return s.split('').reverse().join('') === s
}
export function longestPalindrome(s: string): string {
if (s.length < 1 || s.length > 1000) return ""
if (isPalindrome(s)) return s;
const sArray = s.split('');
const palindromeArr = [];
let auxAcum = "";
sArray.forEach((item)=> {
auxAcum += item;
if(isPalindrome(auxAcum)){
palindromeArr.push({
value: auxAcum,
count: auxAcum.length
});
} else {
if(auxAcum.length > 1) {
const restAuxAcum = auxAcum.slice(1);
if(isPalindrome(restAuxAcum)){
palindromeArr.push({
value: restAuxAcum,
count: restAuxAcum.length
})
}
}
}
})
const resultSorted = palindromeArr.sort((a, b) => b.count - a.count);
return resultSorted[0].value;
}0respuestas