Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const isPalindromic = s => (
s === s.split('').reverse().join('')
);
var longestPalindrome = function(s) {
if (s === '') return '';
let answer = s[0];
for (let i = 0; i < s.length; i+= 1) {
let lastSameStrIndex = s.slice(0, s.length).lastIndexOf(s[i]);
while(lastSameStrIndex !== i) {
const currentStr = s.slice(i, lastSameStrIndex + 1);
if (currentStr.length < answer.length) break;
if (isPalindromic(currentStr)) {
answer = currentStr;
break;
}
lastSameStrIndex = s.slice(0, lastSameStrIndex).lastIndexOf(s[i]);
}
}
return answer;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.