Palindrome Check via Recursion

Easy ⏱ 8 min 86% acceptance ★★★★★ 4.6
Implement is_palindrome(s) that recursively checks whether a lowercase alphanumeric string s reads the same forwards and backwards, by comparing its first and last characters and recursing inward — not by reversing the whole string at once.

Examples

Example 1
Input
is_palindrome('racecar')
Output
True
Explanation

Peels 'r'=='r', then 'a'=='a', then 'c'=='c', leaving the single middle character 'e'.

Example 2
Input
is_palindrome('hello')
Output
False
Explanation

'h' != 'o' at the very first comparison.

Constraints

  • s contains only lowercase letters/digits (no spaces or punctuation).

Topics

RecursionStrings

Companies

TCSWipro

Hints

Hint 1

Base case: len(s) <= 1, return True.

Hint 2

Recursive case: s[0] == s[-1] and is_palindrome(s[1:-1]).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.