Skip to content
BytePatterns

Palindrome Check Ignoring Punctuation

EasyRecursion#tail-recursion#two-pointers~15m

Problem

Decide whether a string reads the same forwards and backwards once you ignore case and skip every character that is not a letter or a digit. Write it first as a recursive function that compares the two ends and recurses on what lies between them, then turn it into a loop that handles strings of any length.

Examples

Input:  s = "A man, a plan, a canal: Panama"
Output: True
Why:    the letters alone read amanaplanacanalpanama both ways
Input:  s = "race a car"
Output: False
Why:    raceacar reversed is racaecar
Input:  s = ".,!"
Output: True
Why:    edge case, nothing is left after skipping punctuation, and an empty string is a palindrome

Hints

0 / 3

Stuck on the idea rather than the code? Tail Calls and Loops covers it.