Created
December 1, 2022 15:50
-
-
Save MohdSaifulM/f7642e28ebf894f85ee6989706e0d4c1 to your computer and use it in GitHub Desktop.
Algorithms - Recursion
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
function isPalindrome(str){ | |
function reverseHelper(str) { | |
// Base case | |
if (str.length === 1) return str; | |
// Recursive case to reverse string | |
return reverseHelper(str.slice(1)) + str[0]; | |
} | |
// Check if reverse of string is same as string | |
return reverseHelper(str) == str; | |
} | |
// isPalindrome('awesome') // false | |
// isPalindrome('foobar') // false | |
// isPalindrome('tacocat') // true | |
// isPalindrome('amanaplanacanalpanama') // true | |
// isPalindrome('amanaplanacanalpandemonium') // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment