Quick Overview

This question evaluates string manipulation, text sanitization, Unicode-aware character classification, and algorithmic complexity reasoning, and falls under the Coding & Algorithms domain.

Determine sanitized palindrome in string

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: HR Screen

Write a function that determines whether a string is a palindrome after removing non-alphanumeric characters and ignoring case (e.g., punctuation, whitespace, symbols). Provide time and space complexity, and include tests covering edge cases with special characters and Unicode.

Overview: This question evaluates string manipulation, text sanitization, Unicode-aware character classification, and algorithmic complexity reasoning, and falls under the Coding & Algorithms domain.

Write a function that determines whether a string is a palindrome after sanitizing it: remove every character that is not alphanumeric (drop punctuation, whitespace, and symbols) and compare letters case-insensitively. Return true if the remaining sequence reads the same forwards and backwards, and false otherwise. An empty result (a string with no alphanumeric characters) is considered a valid palindrome. Example 1: "A man, a plan, a canal: Panama" -> true (sanitizes to "amanaplanacanalpanama"). Example 2: "race a car" -> false (sanitizes to "raceacar"). Example 3: " " -> true (sanitizes to the empty string).

Constraints

  • 0 <= s.length <= 2 * 10^5
  • s consists of printable ASCII characters (and may include Unicode letters/digits).
  • A string with no alphanumeric characters is considered a palindrome.

Examples

Input: ("A man, a plan, a canal: Panama",)

Expected Output: True

Explanation: Sanitizes to 'amanaplanacanalpanama', which reads the same both ways.

Input: ("race a car",)

Expected Output: False

Explanation: Sanitizes to 'raceacar', which is not a palindrome.

Hints

  1. First normalize the string: keep only alphanumeric characters and lowercase them.
  2. After normalizing, a palindrome check is just comparing the sequence to its reverse — or use two pointers moving inward.
  3. Decide your edge-case policy up front: the empty/whitespace-only/punctuation-only cases should all return true.

Determine sanitized palindrome in string

mediumLinkedInSoftware Engineer

Write a function that determines whether a string is a palindrome after sanitizing it: remove every character that is not alphanumeric (drop punctuation, whitespace, and symbols) and compare letters case-insensitively. Return true if the remaining sequence reads the same forwards and backwards, and false otherwise. An empty result (a string with no alphanumeric characters) is considered a valid palindrome.

Example 1: "A man, a plan, a canal: Panama" -> true (sanitizes to "amanaplanacanalpanama"). Example 2: "race a car" -> false (sanitizes to "raceacar"). Example 3: " " -> true (sanitizes to the empty string).

Examples

Example 1
Input
("A man, a plan, a canal: Panama",)
Output
True
Notes
Sanitizes to 'amanaplanacanalpanama', which reads the same both ways.
Example 2
Input
("race a car",)
Output
False
Notes
Sanitizes to 'raceacar', which is not a palindrome.

Constraints

  • •0 <= s.length <= 2 * 10^5
  • •s consists of printable ASCII characters (and may include Unicode letters/digits).
  • •A string with no alphanumeric characters is considered a palindrome.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def solution(s) -> bool:
Sample call
solution("A man, a plan, a canal: Panama")
Language
Case 1
Input
("A man, a plan, a canal: Panama",)
Expected
True
Case 2
Input
("race a car",)
Expected
False

Browse More Questions