Last active
July 13, 2018 17:30
-
-
Save alex-cory/ad2affa76a955d8365d502069da6fd5e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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