JS Built-ins
Algorithmsadvanced

Recursive Palindrome Check

πŸ“– Usage Example
function isPalindrome(s) {
  if (s.length <= 1) return true;
  return s[0] === s[s.length - 1] && isPalindrome(s.slice(1, -1));
}
isPalindrome('racecar'); // true

Check palindromes recursively by comparing outer characters and moving inward.

Recursive Palindrome Check

A palindrome is a string that reads the same forwards and backwards (ignoring spaces and punctuation in real applications).

"racecar" is a palindrome
"hello" is not a palindrome
"a" is a palindrome
"" is a palindrome

Use recursion: if the first and last characters match, recursively check the middle substring.

function isPalindrome(s: string): boolean {
  // Return true if s is a palindrome
}

isPalindrome('racecar') // true
isPalindrome('hello') // false
πŸ’‘ Hints (4)
  • β€’ Base case: if length ≀ 1, return true.
  • β€’ Check if the first and last characters match.
  • β€’ If they match, recursively check the substring without first and last characters using `slice(1, -1)`.
  • β€’ Use `&&` to combine conditions: first check matches AND recursive check.