Frontend School

Validate Alphanumeric Palindrome

DSA
easy

Validate Alphanumeric Palindrome

Easy
String
Two Pointers

Overview

At Salesforce, ensuring data quality and user experience is paramount. When dealing with user-generated content, display names, or records, we often need to validate and normalize text. One common task is checking for palindromic properties, especially when certain characters or casing should be ignored. This problem simulates such a scenario, requiring you to implement a core utility function that can be leveraged in data validation pipelines or UI components.

What you need to do / Task

Implement a function isPalindrome that determines if a given string s is a palindrome. For the purpose of this problem, a string is considered a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forwards and backwards.

Input & output

  • Input: s - A string representing the input text.
  • Output: Returns true if s is an alphanumeric palindrome, false otherwise.

Clarifications

  • An empty string "" is considered a valid palindrome.
  • A string containing only non-alphanumeric characters (e.g., "!@#$") should be treated as an empty string for palindrome check and thus return true.
  • Case should be ignored (e.g., 'A' and 'a' are considered the same character).
  • Only alphanumeric characters (letters and numbers) should be considered for the palindrome check.

Examples

1

Example 1

Input:

s = "A man, a plan, a canal: Panama"

Output:

true

Note:

After filtering and converting to lowercase, the string becomes "amanaplanacanalpanama", which is a palindrome.

2

Example 2

Input:

s = "race a car"

Output:

false

Note:

After filtering and converting to lowercase, the string becomes "raceacar", which is not a palindrome.

3

Example 3

Input:

s = " "

Output:

true

Note:

The string contains only non-alphanumeric characters. After filtering, it becomes an empty string, which is a valid palindrome.

4

Example 4

Input:

s = "Madam"

Output:

true

Note:

After filtering and converting to lowercase, the string becomes "madam", which is a palindrome.

Constraints

Constraints & assumptions

  • 0 <= s.length <= 2 * 10^5
  • s consists only of printable ASCII characters.

Out of scope

  • Handling non-ASCII Unicode characters (e.g., emojis, characters from other languages).
  • Performance optimization for strings exceeding 2 * 10^5 characters, beyond a linear time complexity solution.

What interviewers evaluate

  • Correctness in identifying palindromes under specified rules (alphanumeric, case-insensitive).
  • Efficiency of the algorithm in terms of time and space complexity.
  • Robustness in handling edge cases (empty string, strings with only non-alphanumeric characters).
  • Clarity, readability, and maintainability of the code.

Loading editor...