HomeGorakh Raj Joshi

Using recursion to determine whether a word is a palindrome

A palindrome is a word that is spelled the same forward and backward. For example, rotor is a palindrome, but motor is not.

  • #Recursion
function isPalindrome(str, start = 0, end = str.length - 1) {
  if (start > end) return true;
  if (str[start] !== str[end]) return false;
  return isPalindrome(str, start + 1, end - 1);
}

console.log(isPalindrome('tattat')); // Output: true

Time Complexity O(n)/Space Complexity O(n)

Gorakh Raj Joshi

Senior Fullstack Engineer: Specializing in System Design and Architecture, Accessibility, and Frontend Interface Design

LinkedIn

GitHub

Email

All rights reserved © Gorakh Raj Joshi 2024