Quick Overview

Decide whether a string is a palindrome when spaces, punctuation and letter case are ignored and only letters and digits are compared. It tests string scanning with character filtering, case normalization and edge cases such as strings that contain no letters or digits at all.

Palindrome Check That Ignores Spaces, Symbols and Letter Case

Company: Walmart Labs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Onsite

Given a string, decide whether it reads the same forwards and backwards once spaces and symbols are ignored. The report says only that spaces and symbols must be ignored; this version also ignores letter case, as stated in Rules below. ### Function Signature ```python def is_clean_palindrome(s: str) -> bool: ``` ### Rules - Only ASCII letters (`a` to `z`, `A` to `Z`) and digits (`0` to `9`) are compared. Every other character, including spaces and punctuation, is skipped. - Letters are compared without regard to case: `A` equals `a`. - Return `True` if the sequence of compared characters equals its own reverse, otherwise `False`. A string with no letters or digits counts as a palindrome and returns `True`. ### Constraints - `0 <= len(s) <= 2 * 10^5` - `s` contains only printable ASCII characters (codes 32 through 126). ### Examples **Example 1** ```text Input: s = "Was it a car or a cat I saw?" Output: True ``` The compared characters are `wasitacaroracatisaw`, which reads the same in both directions. **Example 2** ```text Input: s = "12 ab 21" Output: False ``` The compared characters are `12ab21`; the third and fourth characters, `a` and `b`, differ. **Example 3** ```text Input: s = "#! " Output: True ``` No letters or digits remain.

Overview: Decide whether a string is a palindrome when spaces, punctuation and letter case are ignored and only letters and digits are compared. It tests string scanning with character filtering, case normalization and edge cases such as strings that contain no letters or digits at all.

Read the full Walmart Labs Software Engineer interview experience this question came from

Given a string `s`, decide whether it reads the same forwards and backwards once spaces and symbols are ignored. Implement `is_clean_palindrome(s)`. **Rules** - Only ASCII letters (`a` to `z`, `A` to `Z`) and digits (`0` to `9`) are compared. Every other character, including spaces and punctuation, is skipped. - Letters are compared without regard to case: `A` equals `a`. - Return `True` if the sequence of compared characters equals its own reverse, otherwise `False`. A string with no letters or digits counts as a palindrome and returns `True`. The result is a single boolean (`true` / `false` in JavaScript, Java and C++). No value in this problem can exceed 2^31 - 1. **Constraints** - `0 <= len(s) <= 2 * 10^5` - `s` contains only printable ASCII characters (codes 32 through 126). **Example 1** ``` Input: s = "Was it a car or a cat I saw?" Output: True ``` The compared characters are `wasitacaroracatisaw`, which reads the same in both directions. **Example 2** ``` Input: s = "12 ab 21" Output: False ``` The compared characters are `12ab21`; the third and fourth characters, `a` and `b`, differ.

Constraints

  • 0 <= len(s) <= 2 * 10^5
  • s contains only printable ASCII characters (codes 32 through 126).

Examples

Input: ('',)

Expected Output: True

Explanation: Empty string (length 0): nothing is compared, so it counts as a palindrome.

Input: ('a',)

Expected Output: True

Explanation: A single letter is a palindrome.

Hints

  1. Settle first which characters take part in the comparison: only a-z, A-Z and 0-9. Everything else, including characters such as '_', '@' or '[', is ignored.
  2. An uppercase letter and its lowercase form count as the same character. Digits have no case, so '0' never equals 'P'.
  3. If nothing is left once the ignored characters are removed, the answer is True.

Loading coding console...

Show the approach

Approach

Use two indices: i starts at the left end and j at the right end. While i < j: if s[i] is not an ASCII letter or digit, advance i; otherwise, if s[j] is not one, move j left; otherwise both are compared characters, so map uppercase letters to lowercase (digits stay as they are) and return False if they differ, else step both indices inward. Invariant: the compared characters in s[0..i-1] and in s[j+1..n-1] are equal in number, and the k-th compared character from the left equals, ignoring case, the k-th compared character from the right. So the whole compared sequence is a palindrome exactly when the compared sequence of s[i..j] is one. Each step either skips a character that is not compared, which keeps the invariant, or matches the outermost remaining compared pair. A mismatch in that pair proves the sequence is not a palindrome. When i >= j, at most one character is left, which is trivially a palindrome, so True is correct. Edge cases: the empty string and strings with no letters or digits never trigger a comparison and return True. A single compared character is its own mirror. Characters just outside the letter and digit ranges ('/', ':', '@', '[', '`', '{', '_') are skipped, never compared or case-folded. '0' and 'P' are different characters even though their codes differ by 32.

Time complexity:
O(n)
Space complexity:
O(1)