All challenges
Medium 0 solved 0% acceptance

Recursive Palindrome

Check a palindrome recursively, ignoring case and punctuation.

What you'll learn

  • Check palindromes recursively
  • Ignore case and punctuation
  • Shrink the problem from both ends

Problem

Decide whether text reads the same forwards and backwards, considering only letters and digits and ignoring case. Solve it recursively: compare the outer characters, then check the inside.

Implement is_palindrome_recursive(text).

Input

text: The text to check (ASCII characters).

Output

Return true for a palindrome, otherwise false.

Constraints

0 ≤ length ≤ 1,000

Examples

Example 1
Input:
text = "Never odd or even"

Expected output:
expected = True
Input:
std::string text = "Never odd or even";

Expected output:
bool expected = true;
Input:
string text = "Never odd or even";

Expected output:
bool expected = true;

Explanation

Letters only, lowercased: "neveroddoreven" reads the same both ways.

Example 2
Input:
text = "code"

Expected output:
expected = False
Input:
std::string text = "code";

Expected output:
bool expected = false;
Input:
string text = "code";

Expected output:
bool expected = false;

Explanation

"c" and "e" differ, so it is not a palindrome.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Skip non-letter and non-digit characters before comparing the ends.

Hint 2

Compare the outer characters first, then recurse on the remaining middle part.

Hint 3

Handle empty and single-character cleaned substrings as palindromes.

Expected complexity

O(n) time, O(n) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.