Check if a string is a palindrome after removing non-alphanumeric characters

Quick Overview

Decide whether a string reads the same forward and backward once every character that is not a letter or digit is dropped and case is ignored. It tests the two-pointer technique, character filtering, and correct handling of empty or punctuation-only input.

Check if a string is a palindrome after removing non-alphanumeric characters

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Determine if a given string `s` is a palindrome after removing all characters that are not letters or digits and converting all uppercase letters to lowercase. A resulting empty string should be considered a palindrome. ### Function Signature ```python def is_palindrome(s: str) -> bool: ``` ### Rules - Letters are the English letters `a`-`z` and `A`-`Z`, and digits are `0`-`9`. Every other character (spaces, punctuation, symbols) is removed before the check. - An uppercase letter and its lowercase form count as the same character. - Return `True` if the cleaned string reads the same forward and backward, otherwise `False`. ### Constraints - `1 <= len(s) <= 10^5` ### Examples ```text Input: s = "No lemon, no melon" Output: True ``` After removing non-alphanumeric characters and converting uppercase letters to lowercase, the string becomes `"nolemonnomelon"`, which reads the same forward and backward. ```text Input: s = "Hello, world!" Output: False ``` After cleaning and converting, the string is `"helloworld"`. It starts with `h` and ends with `d`, so it is not a palindrome. ```text Input: s = "?" Output: True ``` The only character is removed, leaving the empty string `""`, which counts as a palindrome by definition.

Overview: Decide whether a string reads the same forward and backward once every character that is not a letter or digit is dropped and case is ignored. It tests the two-pointer technique, character filtering, and correct handling of empty or punctuation-only input.

Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Determine if a given string s is a palindrome after removing all characters that are not letters or digits and converting all uppercase letters to lowercase.

A resulting empty string should be considered a palindrome.

Function Signature

def is_palindrome(s: str) -> bool:

Rules

  • Letters are the English letters a - z and A - Z , and digits are 0 - 9 . Every other character (spaces, punctuation, symbols) is removed before the check.
  • An uppercase letter and its lowercase form count as the same character.
  • Return True if the cleaned string reads the same forward and backward, otherwise False .

Constraints

  • 1 <= len(s) <= 10^5

Examples

Input: s = "No lemon, no melon"
Output: True

After removing non-alphanumeric characters and converting uppercase letters to lowercase, the string becomes "nolemonnomelon", which reads the same forward and backward.

Input: s = "Hello, world!"
Output: False

After cleaning and converting, the string is "helloworld". It starts with h and ends with d, so it is not a palindrome.

Input: s = "?"
Output: True

The only character is removed, leaving the empty string "", which counts as a palindrome by definition.