-
-
Save ChickenFur/15041c68ab3603f09f62426aa79b11e6 to your computer and use it in GitHub Desktop.
5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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