Skip to content

Instantly share code, notes, and snippets.

@MohdSaifulM
Created December 1, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohdSaifulM/f7642e28ebf894f85ee6989706e0d4c1 to your computer and use it in GitHub Desktop.
Save MohdSaifulM/f7642e28ebf894f85ee6989706e0d4c1 to your computer and use it in GitHub Desktop.
Algorithms - Recursion
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