Skip to content

Instantly share code, notes, and snippets.

@ChickenFur
Created December 3, 2019 04:09
Show Gist options
  • Save ChickenFur/15041c68ab3603f09f62426aa79b11e6 to your computer and use it in GitHub Desktop.
Save ChickenFur/15041c68ab3603f09f62426aa79b11e6 to your computer and use it in GitHub Desktop.
5
const longestPalindrome = (s: string) => {
const length = s.length;
var result = "";
if (length === 1) {
return s;
}
var fromTheCenter = (left, right) => {
while (left >= 0 && right < length && s[left] === s[right]) {
left--;
right--;
}
return s.slice(left + 1, right);
};
for (var i = 0; i < length - 1; i++) {
var oddPal = fromTheCenter(i, i + 1);
var evenPal = fromTheCenter(i, i);
if (oddPal.length > result.length) {
result = oddPal;
}
if (evenPal.length > result.length) {
result = evenPal;
}
}
return result;
};
console.log(longestPalindrome("nan noon is redder"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment