Skip to content

Instantly share code, notes, and snippets.

@alex-cory
Last active July 13, 2018 17:30
Show Gist options
  • Save alex-cory/ad2affa76a955d8365d502069da6fd5e to your computer and use it in GitHub Desktop.
Save alex-cory/ad2affa76a955d8365d502069da6fd5e to your computer and use it in GitHub Desktop.
// recursive
const isPalindrome = str => {
if (str.length === 0 || str.length === 1) return true
if (str[0] === str[str.length - 1]) return isPalindrome(str.slice(1, str.length - 1))
return false
}
// efficiently in O(n) time
const isPalindrome2 = str => {
var half = Math.floor(str.length / 2)
for (var i = 0; i < half; i++) {
if (str[i] !== str[str.length - i - 1]) return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment